JavaScript String split() method
splits a string into an array of substrings

split() method splits a string into an array of substrings.

syntax

split(separator, limit)

separator The pattern describing where each split should occur.

Can be undefined, a string, or an object with a Symbol.split method - the typical example being a regular expression.

Omitting separator or passing undefined causes split() to return an array with the calling string as a single element.

limit (optional). A non-negative integer specifying a limit on the number of substrings to be included in the array.

If provided, splits the string at each occurrence of the specified separator, but stops when limit entries have been placed in the array. Any leftover text is not included in the array at all.

split() method returns a new array.

split() method does not change the original string.

split() method is case sensitive.

The separator parameter describes where each split should occur and can be a string or regular expression.

If the separator parameter is omitted, an array with the original string is returned.

If an empty string "" character is used as a separator, the string is split between each character.

If a single space " " character is used as a separator, the string is split between words.

If string is comma delimited " , " and is used as a separator, the string is split at the comma.

Any string can be converted into an array by using split() method.

const str = 'abc'; str.split(''); returns ['a', 'b', 'c']

An optional limit may be used to limit the number of splits.

Items after the limit are excluded. If limit is 0 an empty array [] is returned.

syntax 1 description
split myString into a new array of substrings
myString.split(separator)
syntax 2 description
split myString into a new array with set limit of substrings
myString.split(separator, limit)