Latest Code Tutorials

Javascript String Split: How to Split String in JavaScript

JavaScript String split() function is used to split a string into an array of substrings and returns the new array. The split() method does not change the original string.

Javascript string split()

The string split() is a built-in JavaScript function used to split the string into an array of the substring and returns a new collection.  If the empty string (” “) is used as the separator, the string is split between each character. Thus, the split() method does not mutate the original string.


The syntax is following for String Split Example.

string.split(separator, limit)


The first argument to the split function is the string which specifies the points where the split has to take place. 

This argument can be treated as a simple string or as a regular expression. If the separator is unspecified, then the whole string becomes a single array element.

The same thing also happens when a separator is not present in the string. If the separator is an empty string (“”), then every character of the string is separated.

The second argument to a function limit defines an upper limit on the number of splits to be found in a given string. If a string remains unchecked after a threshold or limit is reached, then it is not reported in an array.

Return value

The split() function returns an array of strings that is formed after splitting a given string at each point where the separator occurs.

Let us take an example in Node.js and see how we can use split.

Create one file called the server.js and add the following code.

// server.js

const str = 'Browse and enjoy coding';
const res = str.split(' ', 5);

Now, run the above script using the following command.

Javascript String Split Example Tutorial

So, in the above example, we have split the string with space and the limit is 5.

We can also split the string with a particular letter. Use a letter as a separator. Let us take an example.

// server.js

const str = 'Browse and enjoy coding';
const res = str.split('appdividend');

Javascript String Split Tutorial

So, The split() method splits a String object into an array of strings by separating the string into substrings, using a specified separator string to determine where to do each split.

Let us take another example.

// server.js

const input = 'krunallathiya~25 ankitlathiya~24 rushabhrupani~12345';

const fields = input.split('~');


Destructuring Arrays

We can also the destructure the array after converting the string into an array.

// server.js

const input = 'krunal lathiya~123 Street~Apt 4~New York~NY~12345';

const [name, street, unit, city, state, zip] = input.split('~');

console.log(name); // krunal lathiya
console.log(street); // 123 Street
console.log(unit); // Apt 4
console.log(city); // New York
console.log(state); // NY
console.log(zip); // 12345

That’s it for this tutorial.

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.