Reference

Reference · Strings

String.prototype.split()

split() breaks a string into an array of substrings around a separator. Split on a comma to parse a CSV field, on '' to get characters, or on a regex for flexible delimiters.

XCODXReference · Updated

Definition

String.prototype.split() divides a string into an ordered array of substrings, cutting it at each occurrence of a separator. It is the standard way to turn one string into many pieces: a comma-separated line into fields, a sentence into words, a path into segments. The original string is not modified — split() returns a new array.

The separator is removed from the results — it marks *where* to cut, and does not appear in any piece. Choose the separator to match your data: a literal string for fixed delimiters, or a regular expression when the delimiter varies (any run of whitespace, say). split() is the natural inverse of Array.prototype.join().

Syntax

str.split(separator)
str.split(separator, limit)

// separator: a string or RegExp marking where to cut
// limit:     optional cap on how many pieces to return

Parameters

ParameterRequiredDescription
separatorNoThe string or RegExp to split on. Omit it to get a single-element array holding the whole string; use '' to split into individual characters.
limitNoA non-negative integer capping the number of substrings returned. Extra pieces are discarded, not merged.

When separator is a regular expression that contains capture groups, the captured text is spliced into the output array between the pieces — a lesser-known feature useful for keeping the delimiters you split on. Omitting separator entirely returns [str]: the whole string as the only element.

Return value

A new array of substrings. The separators themselves are not included (unless captured by a regex group). The original string is unchanged.

  • Splitting on '' returns an array of the string's characters.
  • If the separator is not found, the result is a single-element array containing the whole string.
  • Splitting an empty string on '' returns []; splitting it on any other separator returns [''].
  • A separator at the very start or end produces an empty-string element there.

Examples

Split on a delimiter

Split into characters

const chars = 'hello'.split('');
console.log(chars); // ['h', 'e', 'l', 'l', 'o']

Split on a regex (any whitespace)

const messy = 'one   two\tthree';

const words = messy.split(/\s+/);
console.log(words); // ['one', 'two', 'three']

Cap the pieces with limit

const path = 'a/b/c/d';

console.log(path.split('/', 2)); // ['a', 'b'] — extra pieces discarded

Split text into lines

const doc = 'line one\nline two\nline three';

const lines = doc.split('\n');
console.log(lines);        // ['line one', 'line two', 'line three']
console.log(lines.length); // 3

Split, then clean each piece

const csv = 'ada,  bo ,cy';

const names = csv.split(',').map(s => s.trim());
console.log(names); // ['ada', 'bo', 'cy']

Splitting is usually step one of a small pipeline: split() to get the pieces, then map/filter over them. Trimming each piece with .map(s => s.trim()) is almost always what you want after splitting on a comma, because real-world data has stray spaces around the delimiters.

Browser & runtime support

The empty-string edge cases

Splitting is intuitive until the string or the boundaries are empty, and those cases catch people out. ''.split(',') returns [''] — a one-element array holding the empty string, not []. But ''.split('') returns [], because there are no characters to list. And a leading or trailing separator, as in ',a,'.split(','), yields empty-string elements at those positions: ['', 'a', '']. When parsing user input, trim and guard for these rather than assuming every piece is non-empty.

For splitting text into words, a regex separator beats a literal space. split(/\s+/) collapses any run of spaces, tabs or newlines into single cuts, so double spaces don't produce empty-string words. Splitting on a literal ' ' instead would leave an empty string between every pair of adjacent spaces — one of those empty-piece surprises above, just harder to spot in real input.

Availability

split() has been part of the language since the first edition of JavaScript and works in every browser and every maintained version of Node.js. Regex separators and the capture-group splicing behavior are equally long-standing and safe to rely on.

Frequently asked questions

How do I split a string by a comma in JavaScript?
Use str.split(','), which returns an array of the comma-separated pieces. Trim each piece if the input may contain spaces after the commas.
How do I split a string into an array of characters?
Use str.split(''). For full Unicode support with emoji and surrogate pairs, [...str] or Array.from(str) is safer.
What does split() return if the separator isn't found?
A single-element array containing the whole original string, e.g. 'abc'.split(',') returns ['abc'].
Does split() change the original string?
No. Strings are immutable; split() returns a new array and leaves the original string unchanged.