JavaScript

How to split a string using the split() method in JavaScript?

Like any other, high-level programming language JavaScript offers us datatypes where we can store data, and one such data type is a string. A string is simply any textual content enclosed in double or single quotation marks in JavaScript for example “split method” is a string. To manipulate the string data type, there come some inbuilt methods(predefined functionalities) and one such example is the split() method.

What is a split() method?

The split() method divides a specific string into an array of substrings without changing the original string and produces a list of substrings as an array taking two arguments; one is the splitter and the other is the limit.

The splitter acts as the divider and tells the split() method where the specific string should be divided. The splitter can be a regular expression, a single character, or another string. If you don’t provide the splitter in the split() method then the specified original string will be returned.

The limit argument tells the split() method what is the limit where you have to stop the substring process or to put it simply it specifies the number of splits. However, it should be kept in mind that if you don’t give the limit argument to the split method then an empty array will be returned.

Syntax:

myString.split(separator, limit);

Where myString is the specified string and the separator is the splitter and the limit is the specified number of splits.

Split a string using the split() method

Now that we know the split() method splits a specific string into substrings and returns a new array of the substring, let us go through a few examples where we will split a string using the split() method:

Example 1:

In this example, we will split our string into words in the following manner:

var myStr = 'String into words';

var output = myStr.split(' ');

console.log(output);

In the above code, we initialize an array and then apply the split() method on the array. The separator in this case is a space(‘ ‘) which means whenever the split method will see a space, it will divide the string and return the word before the separator.

Example 2:

Let us modify the above example, and return only the first two words in the sentence hence we will use the limit=2 in this example:

var myStr = 'String into words';

var output = myStr.split(' ',2);

console.log(output);

Example 3:

Let us move one step forward and provide a regular expression as a separator. The code will be the same except that we will change the string and provide a separator as an array of regular expression in the argument of the split() method:

var myStr = 'Hello! How are you? My name is Chris';

var output = myStr.split(/[!,?]/);

console.log(output);

As you can see, we provided an array of separators where we defined that if the split() method gets a ! or ? then divide the string. The output of the above code will be:

Conclusion

JavaScript offers us primitive data types which are used for storing data and one such example is a string. The string is some text in double or single quotation marks and it has numerous inbuilt methods. One such method is the split() method that divides a specific string according to a separator into substrings and the substrings array is returned without changing the original string.

In this post, we looked at what is a split() method and how to split a string using the split() method by giving three different examples and screenshots of the output.

About the author

Shehroz Azam

A Javascript Developer & Linux enthusiast with 4 years of industrial experience and proven know-how to combine creative and usability viewpoints resulting in world-class web applications. I have experience working with Vue, React & Node.js & currently working on article writing and video creation.