JavaScript String split()
Syntax & Examples


String.split() method

The split() method of the String class in JavaScript divides a string into an ordered list of substrings, puts these substrings into an array, and returns the array. The division is done by searching for a specified separator string.


Syntax of String.split()

There are 2 variations for the syntax of String.split() method. They are:

1.
split(separator)

Parameters

ParameterOptional/RequiredDescription
separatoroptionalThe pattern describing where each split should occur. Can be a string or a regular expression. If omitted, the entire string is returned in an array with a single element.

This method splits the string at each occurrence of the specified separator and returns an array of substrings.

Returns value of type Array.

2.
split(separator, limit)

Parameters

ParameterOptional/RequiredDescription
separatoroptionalThe pattern describing where each split should occur. Can be a string or a regular expression. If omitted, the entire string is returned in an array with a single element.
limitoptionalA non-negative integer specifying a limit on the number of splits. If provided, only the first limit splits are included in the array.

This method splits the string at each occurrence of the specified separator and returns an array of substrings, limited to the specified number of splits.

Returns value of type Array.



✐ Examples

1 Using split() method with a separator

In JavaScript, we can use the split() method to divide a string into an array of substrings using a specified separator.

For example,

  1. We define a string variable str with the value 'apple,banana,cherry'.
  2. We use the split() method with the separator ','.
  3. The result, an array of substrings, is stored in the variable fruitArray.
  4. We log fruitArray to the console using console.log() method.

JavaScript Program

const str = 'apple,banana,cherry';
const fruitArray = str.split(',');
console.log(fruitArray);

Output

['apple', 'banana', 'cherry']

2 Using split() method with a separator and a limit

In this example, we use the split() method to divide a string into an array of substrings using a specified separator and limit the number of splits.

For example,

  1. We define a string variable str with the value 'apple,banana,cherry,dates'.
  2. We use the split() method with the separator ',' and the limit 2.
  3. The result, an array of substrings limited to two splits, is stored in the variable fruitArray.
  4. We log fruitArray to the console using console.log() method.

JavaScript Program

const str = 'apple,banana,cherry,dates';
const fruitArray = str.split(',', 2);
console.log(fruitArray);

Output

['apple', 'banana']

3 Using split() method with no separator

In this example, we use the split() method without specifying a separator, resulting in an array with the entire string as a single element.

For example,

  1. We define a string variable str with the value 'Hello World'.
  2. We use the split() method without any separator.
  3. The result, an array with the entire string as a single element, is stored in the variable wordArray.
  4. We log wordArray to the console using console.log() method.

JavaScript Program

const str = 'Hello World';
const wordArray = str.split();
console.log(wordArray);

Output

['Hello World']

Summary

In this JavaScript tutorial, we learned about split() method of String: the syntax and few working examples with output and detailed explanation for each example.