The inverse of join, with three special cases worth knowing: no separator at all, an empty-string separator, and an empty input string.
String methodES3 (1999)Live demo
Common call
text.split(',')
Returns
an array of pieces, never a string
Replaces
a manual indexOf-and-slice loop
Watch out
''.split(',') is [''], not [] — length 1
string.split([separator[, limit]])
→ string[]
Demo
Live evaluation
Try:
Inputs
sstringthe string to split
sepstringseparator (may be empty)
Output
'a,b,c'.split(',')
['a', 'b', 'c']
An empty separator splits between every character. Adjacent separators produce an empty string between them — nothing is skipped, which is why a trailing comma gives you a trailing ''. The two empty-input cases are the ones that cause real bugs and they disagree with each other: ''.split(',') gives [''] — an array of LENGTH ONE holding an empty string — while ''.split('') gives a genuinely empty array. Code that checks .length to decide whether there was any input gets the wrong answer from the first one. Finally, when the separator is absent from the string you still get an array, just a one-element one.
Parameters
Name
Type
Required
Description
separator
string | RegExp
no (undefined)
Where to cut. A string matches literally; a RegExp matches by pattern, and any capture groups are INCLUDED in the result. Omitted entirely, the whole string comes back as one element.
limit
number
no (unlimited)
Maximum number of pieces to return. Extra pieces are discarded, not joined onto the last one.
Return value
string[] — An array of substrings. Always an array — for a string with no separator in it, an array of one element.
Common patterns
Parse a delimited line
The everyday use. Trim if the source may have spaces.
constfields = line.split(',').map(f => f.trim());
Split on one of several things
A RegExp separator handles alternatives and runs of whitespace.
constwords = text.split(/\s+/);
Split into characters, safely
Spread respects code points; split("") does not.
constchars = [...text]; // nottext.split('')
Examples
1. Comma separated
'a,b,c'.split(',')
Returns
['a', 'b', 'c']
2. Every character
'abc'.split('')
Returns
['a', 'b', 'c']
3. No separator
'abc'.split()
Returns
['abc']
4. Empty input
''.split(',')
Returns
['']
5. Empty both
''.split('')
Returns
[]
6. Capture group kept
'a1b'.split(/(\d)/)
Returns
['a', '1', 'b']
Pitfalls
1. ''.split(',') is not empty
It returns [''] — one element, length 1. Splitting user input that happens to be blank therefore yields a single empty field rather than no fields, and a .length check reports 1. The inconsistency is real: ''.split('') DOES give [].
Length is 1
''.split(',').length
1
Guard first
constparts = s ? s.split(',') : [];
length 0
2. split('') breaks emoji and other astral characters
It splits by UTF-16 code unit, so anything outside the Basic Multilingual Plane is torn into two useless surrogate halves. Spread syntax and Array.from iterate by code point and keep them whole.
Broken halves
'\u{1F600}x'.split('')
['\ud83d', '\ude00', 'x']
Spread instead
[...'\u{1F600}x']
['\u{1F600}', 'x']
3. A capture group changes the output
With a RegExp separator, anything captured in parentheses is spliced INTO the result array alongside the pieces. Sometimes that is what you want; when it is not, use a non-capturing group.
Separators included
'a1b'.split(/(\d)/)
['a', '1', 'b']
Non-capturing
'a1b'.split(/(?:\d)/)
['a', 'b']
4. limit truncates, it does not group
Unlike the equivalent in Python or Java, the remainder is thrown away rather than returned as a final element. To keep the tail, split once and rejoin, or use indexOf and slice.
Quoted fields containing commas, escaped quotes and embedded newlines all defeat it. It is fine for data you generated yourself, and wrong for anything from a spreadsheet.
Quotes ignored
'a,"b,c"'.split(',')
['a', '"b', 'c"']
Use a parser
parseCsv(line)
['a', 'b,c']
When to use
Use it
Breaking a delimited line into fields
Tokenising on whitespace or a simple pattern
Turning a string into an array to use array methods on it
Taking the part before or after a known delimiter
Reach for something else
Splitting into characters → spread or Array.from, which handle emoji
Real CSV with quoted fields → a parser
You only want the first piece → slice with indexOf
You want to rejoin unchanged → you probably want replace
Notes
Complexity
O(n) in the length of the string, plus regex cost if a pattern is used
Return
A new array of new strings; the original is unchanged (strings are immutable)
Because the algorithm looks for separators in the string, finds none, and returns the whole string as the single piece — and the whole string is ''. The empty-separator case is special-cased in the spec to return [] instead, which is why the two disagree. Guard blank input explicitly rather than trusting .length.