How to Split a String Into an Array in JavaScript

How-to basics · Updated November 22, 2026

Use JavaScript’s String.prototype.split() method to turn a string into an array: for example, 'red,green,blue'.split(',') returns ['red', 'green', 'blue']. The separator tells JavaScript where to divide the text; choose a string for a fixed delimiter or a regular expression for a pattern.

What split() Does

split() is a method on strings. It searches the string for a separator, cuts at each match, and returns the pieces in a new array. The separator itself is not included in the pieces unless it is captured by a group in a regular expression. The original string is unchanged: JavaScript strings are immutable, and split() produces a separate array rather than editing the string in place.

A basic example is a comma-separated list:

const colors = 'red,green,blue'.split(',');
// ['red', 'green', 'blue']

The result is an ordinary array of strings. You can access an item by its index, loop over the items, or use array methods such as map() and filter(). The method is useful for simple text formats, but it does not automatically understand the meaning or structure of the text it splits.

Choose a Separator That Matches the Text

For a known, literal delimiter, pass that delimiter as a string. For example, splitting a path on a slash or a list on a comma is straightforward:

const pathParts = 'docs/reference/split'.split('/');
// ['docs', 'reference', 'split']

const fields = 'Mina|editor|active'.split('|');
// ['Mina', 'editor', 'active']

For a variable amount or type of whitespace, a regular expression is often more suitable. The expression /\s+/ means one or more whitespace characters, so it avoids empty items caused by multiple spaces between words:

const words = 'JavaScript   strings are useful'.split(/\s+/);
// ['JavaScript', 'strings', 'are', 'useful']

Whitespace includes more than the ordinary space character. Depending on the text, it can include tabs and line breaks. If the input has spaces at its beginning or end, this regular expression can still produce an empty first or last item. Use trim() first when removing surrounding whitespace is appropriate:

const words = '  JavaScript   strings  '.trim().split(/\s+/);
// ['JavaScript', 'strings']

Trimming changes the text used for splitting, so do not do it if leading or trailing whitespace is meaningful data.

How Separators and Empty Items Behave

A delimiter match marks a boundary; it is not a command to discard empty fields. Thus, repeated delimiters and delimiters at the ends can produce empty strings in the result:

const parts = 'a,,b,'.split(',');
// ['a', '', 'b', '']

This behavior can matter when fields are positional. In the example, the empty item between the commas represents a missing field, and the final empty string records the trailing delimiter. If an application wants to ignore blank items, filter them explicitly, while remembering that doing so changes the positions:

const nonempty = 'a,,b,'.split(',').filter(part => part !== '');
// ['a', 'b']

Do not filter empty strings automatically when they carry meaning, such as an empty column in a simple delimited record. Also, split() does not trim each item. Splitting 'red, green' on a comma returns ['red', ' green']; trim the individual values if the format permits it.

If the separator is not found, the result contains the whole original string as its only item. Calling split() with no separator, or with undefined, also returns an array containing the entire string. This is different from using an empty-string separator.

Using Regular Expressions

A regular-expression separator can match several possible delimiters or a pattern of variable length. For instance, this splits on commas, semicolons, or pipes:

const items = 'oak;maple,cedar|pine'.split(/[;,|]/);
// ['oak', 'maple', 'cedar', 'pine']

Use a character class when any one character in a set should count as a separator. To split on one or more of those delimiter characters as a single boundary, add +: /[;,|]+/. That can prevent empty items between consecutive delimiters, although it also treats a run of delimiters as one separator.

Regular-expression capturing groups have a special effect: captured text is inserted into the output array. For example:

const pieces = 'one-two'.split(/(-)/);
// ['one', '-', 'two']

If the separator is only meant to divide the text, use a non-capturing group such as (?:...), or use a character class where suitable. Captures can be useful when the delimiter itself must be retained, but they can surprise code that expects every array entry to be a field.

Limit the Number of Results

split(separator, limit) accepts an optional second argument that caps the number of returned array elements. The limit does not mean “split only this many times and then keep the unsplit remainder” in every intuitive sense: it simply truncates the result array to at most that many elements.

const firstTwo = 'north,south,east,west'.split(',', 2);
// ['north', 'south']

A limit of zero returns an empty array. A positive limit can be useful when only the first few fields matter, but it discards the rest of the result. If the final item should contain all remaining text, split first and join the remainder, or write parsing logic that explicitly preserves it.

Splitting Into Characters and Unicode

Passing an empty string as the separator divides a string into UTF-16 code units:

const units = 'cat'.split('');
// ['c', 'a', 't']

For ordinary English letters this often looks like splitting into characters. It is not a reliable way to divide all human-perceived characters, however. JavaScript strings are represented as UTF-16, and some Unicode symbols use a pair of code units. Splitting such a string with split('') can break that pair into invalid halves.

For iteration by Unicode code point, use Array.from() or the string spread syntax instead:

const points = Array.from('A😀');
// ['A', '😀']

const alsoPoints = [...'A😀'];
// ['A', '😀']

Even code points are not always the same as visible characters. A letter combined with a separate accent mark, for example, can consist of multiple code points while appearing as one character. Where the task requires user-perceived grapheme clusters, use Intl.Segmenter with grapheme granularity in supported JavaScript environments rather than assuming that split(''), spread syntax, or Array.from() does that job.

Common Mistakes and Data-Format Limits

  • Using the wrong delimiter: 'a,b'.split(';') returns ['a,b'], because there is no semicolon to match.
  • Expecting trimming: splitting around a comma does not remove spaces around each resulting value.
  • Forgetting empty fields: repeated or trailing delimiters can create empty strings.
  • Expecting the separator in the output: a string separator is discarded; only captured regular-expression groups are included.
  • Treating a simple split as a full parser: delimiters inside quoted fields need special handling.

For example, splitting '"North, District",West' on commas breaks the quoted first field into two pieces, even though the comma is part of its value. split() does not parse CSV quoting rules, escape sequences, nested structures, or field types. For CSV or another formal data format, use a parser that implements that format’s rules. A plain split is appropriate only when the input’s delimiter rules are genuinely simple and controlled.

Alternatives to split()

The best alternative depends on what the result should represent:

  • match(): use it when you want to collect text matching a pattern rather than cut around separators. For example, 'a1b22'.match(/\d+/g) returns the digit sequences ['1', '22'].
  • Array.from() or spread syntax: use these to iterate a string by Unicode code point rather than split it at a delimiter.
  • Intl.Segmenter: consider it when segmentation must respect grapheme clusters, words, or sentences according to locale-aware rules.
  • A parser: use a dedicated parser when input has quoting, escaping, nesting, or a documented format grammar.

These options are not interchangeable. split() is usually clearest for a known boundary; a regular expression is suitable for pattern-based boundaries; and a parser is safer when the input format has rules that a delimiter alone cannot express.

Frequently Asked Questions

How do I split a string by spaces in JavaScript?
Use text.split(' ') for exactly one ordinary space as the delimiter. Use text.trim().split(/\s+/) when runs of whitespace should count as one separator and surrounding whitespace should be ignored.

Does split() change the original string?
No. Strings are immutable in JavaScript. split() returns a new array and leaves the original string value unchanged.

How can I split a string into an array of characters?
text.split('') works for many basic strings but can break Unicode characters represented by surrogate pairs. Use Array.from(text) or [...text] to iterate by code point; use Intl.Segmenter when you need grapheme clusters.

Why does split() return an empty string?
An empty item usually means two delimiters were adjacent, or a delimiter appeared at the start or end of the input. Check whether that empty field is meaningful before filtering it out.

How do I split on more than one delimiter?
Pass a regular expression, such as text.split(/[;,]/) to divide on commas or semicolons. Add a quantifier such as + if consecutive delimiter characters should be treated as one boundary.

Can split() parse CSV?
Not reliably when fields can contain quoted delimiters, escaped quotes, or line breaks. A comma split only recognizes commas; use a CSV parser for data that follows CSV rules.

Related

Try it: the Date Difference Calculator: days, months, years and weekdays between two dates, with the DATEDIF formulas.

More in How-to basics

Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources

Spotted an error in this guide? Let us know.