What is Split() Method and How to Split a String In JavaScript

What is a split function and how we can split a string in JavaScript, In this complete article we learn the JavaScript split function with the help of different examples. So, before we jump into the code, we have to know more about split function and syntax.

Introduction to JavaScript Split String

The split() function in JS split the string into an array of smaller strings and returns a new array. You can get the value using the array index after using the split function.

Syntax of JS Split Function

The separator is an optional parameter but you can set any regular expression like comma, colon, and space, etc, or any character to split the string.

The limit is also an optional parameter, It takes an integer value which sets the limit of splitting on the given string.

Some major points to know about split string in JavaScript,

  1. If you use an empty parameter as separator like string.split(""), then it will split all the character from the string.
  2. Suppose you set a space as separator in the split function string.split(" "), then function check the spaces in the statement of string and split by spaces.
  3. Now, if you use 2 as limit and space as separator string.split(' ',2), then it check space and take 2 string from the statement or sentance.

Example of JavaScript Split String

In the above example, we split a string into an array by using space as a separator.

Then split function converts the complete string statement as an array.

Then we get the value by using the index of an array.


The output of a Split function
The output of a Split function

This example also satisfies the query for “JavaScript split string by space”.

JavaScript Split String Into an Array By Comma

In this example, we print the complete array that is created by the JS split function with the help of a comma separator.


The output of Split String Into an Array By Comma
The output of Split String Into an Array By Comma

By Using Limit Parameter in JavaScript Split Function

In the above example, we use space as a separator and 3 as a limit parameter.

It will only checks 3 strings and prints an array of those string.


The output of limit parameter in split JavaScript function
The output of limit parameter in split javascript function

Splitting String by Regular Expression (RegEXP) using JS Split Function


In the above code, we set the regular expression as a separator and split the complete statement as an array by using the JavaScript split function.

You can run all the code here JSFiddle – Code Playground and Run PHP Code (phpcoder.tech).


Here we discuss complete source code examples of the JavaScript Split function. How we can use the live projects in various ways.

Also, discuss important points to remember about the JS Split function.

I hope you all understand the use of the split function. If you face any issues please let me know.

Happy Coding..!

Was this article helpful?
My name is Bikash Kr. Panda. I own and operate PHPCODER.TECH. I am a web Programmer by profession and working on more than 50 projects to date. Currently I am working on the web-based project and all the CMS and frameworks which are based on PHP.
Posts created 188

One thought on “What is Split() Method and How to Split a String In JavaScript

Leave a Reply

Your email address will not be published.

Related Posts

Begin typing your search term above and press enter to search. Press ESC to cancel.

Back To Top