Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / JavaScript

String.prototype.split — splitting text into an array

String.prototype.split divides text at a separator and returns an array of parts. Learn how limits and empty elements work, and how an empty separator differs from an omitted separator.

Version: ECMAScript and Web APIs; exact feature edition and browser support reviewed per entry; Node 24 in repository does not prove browser API support

Syntax

string.split(separator, limit)

String.prototype.split divides text from left to right without changing the original. A string separator is matched literally and excluded from the resulting array. Adjacent separators and separators at the beginning or end can produce empty elements.

An omitted separator or undefined returns an array containing the entire string, unless the limit is zero. No match also leaves the entire string intact. The separator "" splits into UTF-16 code units, not necessarily complete user-perceived characters; "".split("") returns [].

The optional limit caps the number of elements, not the number of splits: the remaining text is discarded. Its default is 2 ** 32 - 1; a supplied value is converted through ToUint32. A limit of 0 produces [].

The separator can also be an object with a [Symbol.split] method, such as a RegExp. In that case, split passes the receiver and limit to that method and returns its result directly; a custom method can change the rules described above. Calling String.prototype.split with a null or undefined receiver throws TypeError.

Examples

const text = "red,,blue,";
console.log(JSON.stringify(text.split(",")));
console.log(JSON.stringify(text.split(",", 2)));
console.log(JSON.stringify(text.split()));
console.log(JSON.stringify("".split("")));

Result

["red","","blue",""]
["red",""]
["red,,blue,"]
[]

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources