References

Beginner-friendly references for web development, with live, editable examples.

The JavaScript string.split() method

Method JavaScript All modern browsers Updated
Quick answer

The split() method breaks a string into an array of pieces, cutting at each occurrence of a separator. "a,b,c".split(",") returns ["a", "b", "c"]. Split on "" to get individual characters, or on " " to get words. It's the reverse of join().

Overview

split() turns a string into an array by chopping it wherever it finds the separator you give it. Comma-separated data, a sentence into words, a path into segments — all one call. The separator can be a string or a regular expression, and it isn't included in the results, just used as the cut point.

Two special cases come up constantly. Splitting on an empty string, split(""), gives you an array of individual characters. Splitting on a space, split(" "), gives you words. There's also an optional second argument, a limit, that caps how many pieces you get back — handy when you only need the first few.

It pairs naturally with its opposite, join(): split breaks a string apart, join glues an array back together. A very common pattern is to split, transform the pieces with map(), and join them back. Note that for real character-by-character work with emoji and other astral characters, spreading ([...str]) is safer than split("").

Syntax

const parts = str.split(separator)
const parts = str.split(separator, limit)

"a,b,c".split(",")   // ["a", "b", "c"]
"hello".split("")    // ["h","e","l","l","o"]

Parameters

The string.split() method accepts the following parameters.

Parameter Description
separator The string or regular expression to break on. It is not included in the result. An empty string splits into characters.
limit Optional. The maximum number of substrings to return. Extra pieces are discarded.

Example

Live example
<pre id="out" style="font:15px ui-monospace,monospace"></pre>
<script>
  const csv = 'apple,banana,cherry';

  const fruits = csv.split(',');

  document.getElementById('out').textContent =
    'count: ' + fruits.length + '\n' +
    'first: ' + fruits[0]; // count: 3 / first: apple
</script>

Best practices

  • Split on "" for characters and " " (or /\s+/) for words.
  • Pair split() with join() to transform text: split, map, then join back.
  • Pass a limit when you only need the first few pieces.
  • For emoji-safe character splitting, use the spread operator [...str] instead of split("").

Frequently asked questions

How do I split a string in JavaScript?
Use str.split(separator), e.g. "a,b,c".split(",") returns ["a", "b", "c"].
How do I split a string into characters?
Split on an empty string: "abc".split("") gives ["a", "b", "c"]. For emoji safety, prefer [..."abc"].
How do I split a string into words?
Split on a space, str.split(" "), or on whitespace with a regex, str.split(/\s+/), to handle multiple spaces.
What is the difference between split() and join()?
split() turns a string into an array; join() turns an array into a string. They're opposites.