String.prototype.split()
Splits a string into an array of substrings using a separator.
Syntax
str.split(separator, limit) Parameters
| Name | Type | Required | Description |
|---|---|---|---|
separator | string | RegExp | No | The delimiter to split on. If omitted, the whole string becomes a single-element array. |
limit | number | No | Maximum number of substrings to return. |
Returns
Array — An array of substrings.
Examples
console.log('a,b,c'.split(','));
Output
[ 'a', 'b', 'c' ]
console.log('hello'.split(''));
Output
[ 'h', 'e', 'l', 'l', 'o' ]
Notes
Splitting on an empty string splits by UTF-16 code unit (breaks emoji); use
`[...str]` or `Array.from(str)` for code-point-safe splitting. Does not mutate
the original. The inverse of `Array.prototype.join`.
Browser & runtime support
| Environment | Since version |
|---|---|
| chrome | 1.0 |
| firefox | 1.0 |
| safari | 1.0 |
| edge | 12 |
| node | 0.10 |