String.split()
Splits a string into an array using a separator or regex.
string.split(separator?, limit?)This static page keeps the syntax and examples indexed for search, while the coding app handles interactive exploration and saved references.
What it does
Overview
Splits a string into an array using a separator or regex.
String.prototype.split() partitions a string into an ordered list of substrings by searching for a pattern, which can be a simple string or a regular expression. The resulting substrings are returned as an array. If the separator is omitted or does not appear in the string, the returned array contains one element: the original string. When an empty string is used as a separator, the method splits between every UTF-16 code unit, which can lead to issues with surrogate pairs such as emojis or specific mathematical symbols. Performance considerations: split() is generally O(N) where N is the length of the string, but using complex regular expressions with capturing groups increases overhead because the captured matches are injected into the resulting array. The 'limit' parameter restricts the number of entries in the array, but developers should be aware that the engine may still process the entire string depending on the implementation. Note that split() does not mutate the original string.
Quick reference
Syntax
string.split(separator?, limit?)
Inputs
Parameters
See it in practice
Examples
Basic String Delimiter
var text = 'JavaScript,Python,Rust';
var result = text.split(',');['JavaScript', 'Python', 'Rust']
Splits a CSV string into an array using a comma as the delimiter.
Regex with Capturing Groups
var text = 'Hello 123 World';
var result = text.split(/(\d+)/);['Hello ', '123', ' World']
When using a regular expression with capturing groups, the matched separators are included in the resulting array.
Using the Limit Parameter
var text = 'apple-orange-pear-grape';
var result = text.split('-', 2);['apple', 'orange']
The limit parameter '2' stops the split operation once two elements have been added to the array.
Debug faster
Common Errors
TypeError
Cause: Attempting to call .split() on a value that is null or undefined.
Fix: Ensure the target variable is a string using a typeof check or conversion.
var data = null;
var parts = data.split(',');LogicError
Cause: Using split('') on strings containing multi-byte characters (Unicode surrogate pairs).
Fix: Use Array.from(string) or the spread operator [...string] to correctly handle emojis and special symbols. Note: IE not supported for Array.from.
var emojiStr = 'A\uD83D\uDE00B'; // A😀B
var result = emojiStr.split(''); // Breaks the emoji surrogate pairRuntime support
Compatibility
Source: MDN Web Docs
Common questions
Frequently Asked Questions
Splits a string into an array using a separator or regex.
separator: Where to split the string. limit: Max number of pieces to return.
TypeError: Ensure the target variable is a string using a typeof check or conversion. LogicError: Use Array.from(string) or the spread operator [...string] to correctly handle emojis and special symbols. Note: IE not supported for Array.from.