String.split() in JavaScript: cutting a string into an array

split cuts a string at every separator it meets and hands back an array. The separator can be plain text or a regular expression.
3 min read
Believemy logo

A line from a CSV file, a list of keywords separated by commas, a date written 2026-08-20: text almost always arrives with a structure hidden inside it.

split is the tool that brings it out, turning a String into a workable Array.


Definition

split(separator) cuts the string everywhere the separator appears and hands back an array of the pieces. The separators themselves do not show up in the result, and the original string is not changed.

JAVASCRIPT
const header = "name,email,country";
console.log(header.split(","));  // [ 'name', 'email', 'country' ]

const [year, month, day] = "2026-08-20".split("-");
console.log(day);  // "20"

console.log("no comma here".split(","));  // [ 'no comma here' ]

The second line pairs split with Destructuring, which gives three named variables in a single statement. The third one is a useful reminder: when the separator is missing, the result is a one-item array, never an empty one.


Cutting with a regular expression

A separator can also be a pattern, which covers the cases where spacing is irregular or where several symbols separate the values.

JAVASCRIPT
console.log("a b   c".split(" "));    // [ 'a', 'b', '', '', 'c' ]
console.log("a b   c".split(/\s+/));  // [ 'a', 'b', 'c' ]

console.log("2026-08-20T10:30".split(/[-T:]/));  // [ '2026', '08', '20', '10', '30' ]

The first line shows what a literal separator does: two consecutive spaces produce an empty string between them. The \s+ pattern, meaning "one or more whitespace characters", settles it. Patterns are covered in RegExp.


The limit, and the empty-separator trap

An optional second argument caps how many pieces come back. The rest of the string is dropped, it is not folded into the last item.

JAVASCRIPT
console.log("fr,en,es,de".split(",", 2));  // [ 'fr', 'en' ]

console.log("abc".split(""));   // [ 'a', 'b', 'c' ]
console.log("a👍".split(""));   // three items, the emoji is cut in half
console.log([..."a👍"]);        // [ 'a', '👍' ], two items
Good to know

Splitting on "" works on code units, not on visible characters. Emoji and some writing systems take up two units and end up cut into unreadable pieces. To get the real characters, use Spread (...) or Array.from.


Frequently asked questions

Question

How do you rebuild a string from the array?

With join, the reverse operation: ["fr", "en"].join(", ") hands back "fr, en". The split then join round trip is the shortest way to normalize a separator, replacing semicolons with commas for instance.


Question

Can a real CSV file be cut with split?

On simple data, yes. As soon as a field holds a comma inside quotes, a line break or an escaped quote, the naive split breaks silently. The format is subtler than it looks, and a dedicated library saves a whole category of data bugs.


Question

What does split hand back with no argument at all?

An array holding the whole string, as a single item. That is the case to know when the separator comes from a variable that can be undefined: instead of an error, you receive an array that looks right and a process that cuts nothing.

Related terms

Discover our javaScript glossary

Every word of JavaScript explained simply: keywords, built-in objects, methods, errors and concepts. Clear definitions and examples that actually run, to learn and to troubleshoot.

Share this article

Want to help us? Share this article on your networks or even better: on your site, in an article or in your newsletter.