Skip to main content

What the split() method does

split() is a string method (not an array one) that divides a string into pieces at the separator you specify and returns an array of substrings. The source string stays untouched, because strings in JavaScript are immutable by nature, and the method simply builds a new array.

Theory

TL;DR

  • split() lives on strings: "abc".split(...), never on arrays.
  • It returns an array of substrings and does not change the source string.
  • The separator can be a string or a regular expression.
  • The second parameter limit caps how many elements the result holds.
  • If the separator is not found, you get a one element array.
  • The reverse operation is join().

Quick example

javascript
const str = "apple,banana,cherry"; const arr = str.split(","); console.log(arr); // ["apple", "banana", "cherry"]
  • The separator "," says where to cut the string.
  • The result is an array of strings with the commas gone.

Syntax and parameters

javascript
string.split(separator, limit);
ParameterDescription
separator(optional) the string or regular expression to split on. If omitted, the method returns the whole string as a single element.
limit(optional) the maximum number of elements in the result.

limit truncates the result rather than keeping the remainder: "a,b,c,d,e".split(",", 3) gives ["a", "b", "c"] and the tail "d,e" is simply lost. When you need the remainder intact, cut manually with indexOf and slice.

Examples

Splitting on spaces:

javascript
const text = "JavaScript is awesome"; const words = text.split(" "); console.log(words); // ["JavaScript", "is", "awesome"]

Splitting into characters:

javascript
const chars = "hello".split(""); console.log(chars); // ["h", "e", "l", "l", "o"]

With a limit:

javascript
const str = "a,b,c,d,e"; const parts = str.split(",", 3); console.log(parts); // ["a", "b", "c"]

When the separator is not found:

javascript
const str = "apple banana"; console.log(str.split(",")); // ["apple banana"], nothing changed

When the string is empty:

javascript
"".split(","); // [""] "".split(""); // [], the only case that yields an empty array

A regular expression as the separator

When there are several separators or they vary, pass a regular expression instead of a string:

javascript
"one1two22three".split(/\d+/); // ["one", "two", "three"] "a, b ,c".split(/\s*,\s*/); // ["a", "b", "c"], spaces around commas are eaten "line1\r\nline2\nline3".split(/\r?\n/); // ["line1", "line2", "line3"]

If the regular expression contains a capturing group, what it captured ends up in the result too, which is sometimes exactly what you want:

javascript
"2025-10-14".split(/([-])/); // ["2025", "-", "10", "-", "14"]

The split() and join() pair

These two almost always travel together: split() takes a string apart, join() puts it back.

javascript
const text = "JS is great"; const reversed = text.split(" ").reverse().join(" "); console.log(reversed); // "great is JS"

Step by step:

  • split(" ") turns the string into an array of words;
  • reverse() flips the order;
  • join(" ") collects the array back into a string.

The same trick gives the shortest way to reverse a string: str.split("").reverse().join("").

In brief

What it doesSplits a string into pieces and returns an array
Where it livesOn strings only ("abc".split())
ReturnsAn array
Changes the source stringNo
Example"1,2,3".split(",") gives ["1", "2", "3"]

Summary: split() is the way to turn a string into an array using a separator of your choice.

Common mistakes

  • Calling split() on an array. It is a string method: [1,2,3].split(",") throws a TypeError. Arrays are turned into strings by join().
  • Confusing split() with splice() and slice(). split() goes string to array; slice() copies a part; splice() removes or inserts inside an array and mutates it.
  • Expecting "".split(",") to be an empty array. It returns [""], an array holding one empty string, so a parts.length check on empty input gives 1, not 0.
  • Splitting a string with emoji via split(""). That walks UTF-16 code units and cuts surrogate pairs in half. Prefer [...str] or Array.from(str), which split by code points.
  • Parsing CSV with split(","). A comma inside quotes breaks the parse; real CSV needs a parser, not a single method call.
  • Forgetting to trim. "a, b".split(",") gives ["a", " b"] with a leading space. Either add .map(s => s.trim()) or split on the regular expression /\s*,\s*/ from the start.

Short Answer

Interview ready
Premium

A concise answer to help you respond confidently on this topic during an interview.