Split a JavaScript string into an array with split()
text.split(separator, limit) returns an array of parts. A string separator is removed from the results, and the source is unchanged. If the separator is absent or omitted, the result contains the whole string.
Split where a separator occurs
const text = 'abc,def,ghi';
console.log(text.split(',')); // ['abc', 'def', 'ghi']
console.log(text.split('def')); // ['abc,', ',ghi']
console.log(text.split('|')); // ['abc,def,ghi']
console.log(text);
Limit the number of array elements
limit is the maximum array length. A limit of 2 discards remaining parts and 0 returns an empty array. Repeated delimiters or delimiters at the ends can yield empty parts. Use filter(part => part !== '') to drop them and map(part => part.trim()) to trim them when needed. See String.prototype.split().
console.log('abc,def,ghi'.split(',', 2)); // ['abc', 'def']
console.log('abc,def,ghi'.split(',', 0)); // []
console.log('abc,def,ghi'.split()); // ['abc,def,ghi']
Keep all text after the first separator
Consider region:eu:west. split(':', 2) drops west. Use indexOf() and slice at the first colon to preserve the entire value.
{
function splitAtFirstColon(text) {
const index = text.indexOf(':');
if (index === -1) return [text];
return [text.slice(0, index), text.slice(index + 1)];
}
const setting = 'region:eu:west';
console.log(setting.split(':', 2)); // ['region', 'eu']
console.log(splitAtFirstColon(setting)); // ['region', 'eu:west']
console.log(splitAtFirstColon('region')); // ['region']
}
The outputs are ['region', 'eu'], ['region', 'eu:west'], and ['region']. A missing colon gives -1, so the helper returns just the original text. With a longer separator, advance by its full length.
Try a complete separator and limit example
Save the code as split-example.html in UTF-8 and open it. The initial result contains abc, def, and ghi. Set the maximum to 2 for abc and def, or 0 for an empty array. A separator that does not occur leaves the string in one element. Reset restores all fields.
The form accepts integer limits from 0 to 4,294,967,295. For a string separator, the API converts an explicit limit to an unsigned 32-bit integer, so a larger value can wrap to a smaller one.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Practice splitting strings</title>
</head>
<body>
<label for="source">Original string</label>
<input id="source" type="text" value="abc,def,ghi">
<label for="separator">Separator</label>
<input id="separator" type="text" value=",">
<label for="limit">Maximum parts (blank for all)</label>
<input id="limit" type="number" min="0" max="4294967295" step="1">
<button id="run" type="button">Split</button>
<button id="reset" type="button">Reset</button>
<pre id="output" aria-live="polite"></pre>
<script>
const source = document.querySelector('#source');
const separator = document.querySelector('#separator');
const limit = document.querySelector('#limit');
const output = document.querySelector('#output');
function show() {
const limitText = limit.value.trim();
const n = Number(limitText);
if (limit.validity.badInput || (limitText !== '' && (!Number.isInteger(n) || n < 0 || n > 0xFFFFFFFF))) {
output.textContent = 'Enter an integer from 0 to 4,294,967,295.';
return;
}
const parts = limitText === ''
? source.value.split(separator.value)
: source.value.split(separator.value, n);
output.textContent = `Result: ${JSON.stringify(parts)}\nNumber of parts: ${parts.length}`;
}
document.querySelector('#run').addEventListener('click', show);
document.querySelector('#reset').addEventListener('click', () => {
source.value = 'abc,def,ghi';
separator.value = ',';
limit.value = '';
show();
});
show();
</script>
</body>
</html>
Watch for Unicode, regex captures, and CSV
An empty delimiter splits UTF-16 (16-bit Unicode Transformation Format) code units and can divide an emoji. Array.from(text) iterates code points. To keep user-perceived characters together, use Intl.Segmenter with grapheme segmentation.
console.log('a,,b,'.split(',')); // ['a', '', 'b', '']
console.log('a, b; c'.split(/[,;]\s*/)); // ['a', 'b', 'c']
console.log(Array.from('\u{1F642}'));
A regex delimiter can include captured text in the result. For comma-separated values (CSV), use a parser that understands quotes when fields can contain commas. See RFC 4180.




