JavaScript String split()

The split() method divides a string into a list of substrings and returns them as an array.

Example


split() Syntax

The syntax of split() is:

str.split(separator, limit)

Here, str is a string.


split() Parameter

The split() method takes in:

  • separator (optional) - The pattern (string or regular expression) describing where each split should occur.
  • limit (optional) - A non-negative integer limiting the number of pieces to split the given string into.

split() Return Value

  • Returns an Array of strings, split at each point where the separator occurs in the given string.

Note: The split() method does not change the original string.


Example: Using split()

Output

[ 'A', 'B', 'C', 'D', 'E', 'F' ]
[ 'Java is awesome', ' Java is fun', '' ]
[ 'Java is awesome', ' Java is fun' ]
[ 'JavaScript ', '  Python ', 'C', 'C++' ]
[ 'JavaScript', 'Python', 'C', 'C++' ]

Note: If the separator is a regular expression with capturing parentheses, then each time the separator matches, the results of the capturing parentheses are spliced into the output array.


Recommended Reading: JavaScript Array join()