JavaScript split() to Array: How It Works and Examples

How-to basics · Updated November 22, 2026

Yes. JavaScript’s split() method converts a string into an array of substrings, using a separator you specify. For example, 'red,blue,green'.split(',') returns ['red', 'blue', 'green']; the original string is not changed.

What split() Does

split() is a method of JavaScript strings. It searches the string for a separator, divides the string at each match, and returns the pieces in a new array. The separator is not included in the result unless it is captured by a regular expression.

This is useful whenever a string represents several values joined by a known boundary: a comma-separated list, a line of text, a path divided by slashes, or words separated by spaces. The method returns an array even if it finds no separator. In that case, the array usually contains the entire original string as its only element.

For example, if colors contains 'red,blue', then colors.split(',') produces a separate array with two entries. The value in colors remains 'red,blue'. Since strings are immutable in JavaScript, splitting does not edit the source string.

Basic Syntax and Examples

The method accepts a separator and, optionally, a limit on the number of array elements:

string.split(separator, limit)

The separator can be a string or a regular expression. It can also be omitted. These simple examples show the most common results:

  • 'a-b-c'.split('-') returns ['a', 'b', 'c'].
  • 'one two three'.split(' ') returns ['one', 'two', 'three'].
  • 'first,last'.split(',') returns ['first', 'last'].
  • 'hello'.split() returns ['hello'], because no separator was provided.

Notice that a separator must match what is actually in the string. Splitting 'one, two, three' on a plain comma gives ['one', ' two', ' three'], with a leading space in the second and third entries. A comma is not the same separator as a comma followed by a space.

Choosing a String or Regular Expression Separator

Use a string separator when the boundary is a fixed, literal sequence. For instance, '2026-11-03'.split('-') divides a date at each hyphen. A string separator is not treated as a pattern: characters such as a period or question mark have no special regular-expression meaning when supplied as a string.

Use a regular expression when separators can vary. To split a sentence at one or more whitespace characters, pass a regular expression that matches whitespace:

'one two\tthree'.split(/\s+/)

This returns ['one', 'two', 'three']. The \s pattern matches whitespace, and the + means one or more matches. This is often more reliable than splitting on a single space when input may contain repeated spaces or tabs.

Regular expressions with capturing parentheses have a noteworthy behavior: captured separator text is included in the resulting array. For example, 'a1b2c'.split(/(\d)/) returns ['a', '1', 'b', '2', 'c']. If you do not want separators in the array, use a non-capturing group where appropriate, written as (?:...), or choose a pattern without capturing parentheses.

Empty Separators and Empty Strings

An empty string separator, '', splits a string between its UTF-16 code units. For ordinary English text, that often looks like one array entry per character: 'cat'.split('') returns ['c', 'a', 't']. This behavior can be useful for simple character-level work, but it has an important limitation for Unicode text.

JavaScript strings are represented using UTF-16 code units. Some visible symbols, including many emoji, use a pair of code units. Splitting with '' can separate that pair and produce pieces that are not valid standalone representations of the symbol. To iterate by Unicode code point instead, use Array.from(text) or [...text]. Those approaches still do not necessarily treat a combined visible character, such as a letter plus a combining accent or a joined emoji sequence, as one grapheme cluster.

An empty string as the input is another case worth checking. ''.split(',') returns [''], a one-element array containing an empty string. By contrast, ''.split('') returns an empty array. If your program needs to distinguish an empty input from a missing value or a list with no entries, test for that condition explicitly.

Using the Limit Argument

The optional second argument limits how many elements the returned array contains. For example, 'a,b,c,d'.split(',', 2) returns ['a', 'b']. The limit applies to the number of output entries, not to the number of separator matches to process in a separate operation. The remaining text is not added as a final combined entry.

A limit of zero returns an empty array: 'a,b'.split(',', 0) produces []. This can be useful when a caller supplies a maximum number of fields, but it is not a way to preserve the unprocessed remainder. If you need a fixed number of fields plus the rest of the text, split first and combine the remaining entries with another operation such as slice() and join().

Limits are especially useful when only the first part matters. For instance, splitting a value on a colon with a limit of two can give you the first field and the next piece, but it will discard later output entries rather than automatically joining them back together. Confirm the intended behavior with a small example before using a limit on structured input.

Whitespace, Lines, and Common Parsing Tasks

Whitespace handling is a common source of unexpected array values. Splitting on one literal space preserves empty entries caused by repeated spaces: 'red blue'.split(' ') returns ['red', '', 'blue']. If the goal is to separate words regardless of how much whitespace is between them, a regular expression is usually a better fit:

' red blue '.trim().split(/\s+/)

This returns ['red', 'blue']. The call to trim() removes whitespace at the two ends first; without it, splitting the leading whitespace would create an empty first entry. If empty fields are meaningful in your data, do not trim or filter them without considering what they represent.

To split text into lines, a pattern can account for both common newline conventions: text.split(/\r?\n/). It handles line feeds and carriage-return-plus-line-feed sequences. A trailing newline can leave an empty final element, which may or may not be desirable. For large files or more involved text processing, a dedicated line-reading approach may be more suitable than loading and splitting the entire string.

For a simple list such as 'tea,coffee,water', splitting on commas is reasonable. But split(',') is not a complete CSV parser. Real CSV data can contain quoted fields with commas, quotation marks, or line breaks inside them. A basic split would break those fields incorrectly; use a CSV parser when processing general CSV files.

Common Mistakes and How to Avoid Them

  • Expecting split to mutate the string: It returns a new array. Store or use that return value; calling text.split(',') without using the result does not change text.
  • Using the wrong separator: A comma and a comma-plus-space produce different results. Inspect the input, including whitespace and punctuation, before choosing the separator.
  • Assuming an unmatched separator gives an empty array: If the separator is not found, the result is normally an array containing the original string, such as 'hello'.split(',') returning ['hello'].
  • Forgetting empty fields: Leading, trailing, or repeated separators can create empty strings. For example, ',a,'.split(',') returns ['', 'a', '']. Decide whether those empty values should be kept, removed, or treated as errors.
  • Calling split on a non-string: Numbers and other values do not automatically have the string method. Convert deliberately with String(value) when conversion is appropriate, and check for null or undefined if the value may be absent.

Filtering empty entries can be appropriate for a whitespace-separated list, for example, but it can be harmful when empty fields carry meaning. A blank column in a delimited record may indicate missing data at a specific position. Preserve the array structure until you know what the input format requires.

split() Compared with Other Ways to Make Arrays

split() is for dividing a string around a separator. It differs from Array.from() and the spread syntax, which turn an iterable string into an array of Unicode code points rather than finding words or fields. For example, Array.from('a,b') returns ['a', ',', 'b']; it does not treat the comma as a boundary to remove.

After splitting, array methods can transform or inspect the parts. A common pattern is text.split(',').map(item => item.trim()), which splits on commas and removes surrounding whitespace from each resulting item. This is suitable for a simple list, but it does not solve CSV quoting. Use join() for the reverse kind of operation: it combines array elements into a string with a chosen separator, though the original formatting cannot always be reconstructed.

If a string represents structured data, choose a parser made for that format rather than guessing with separators. JSON text, for example, should be read with JSON.parse(), not split on commas or colons. A format-specific parser understands nesting and escaping rules that a simple separator operation does not.

Frequently Asked Questions

Does JavaScript split() return an array?

Yes. It returns a new array containing the parts of the string. If the separator does not appear, the array usually has one element: the original string.

How do I split a string by spaces in JavaScript?

For exactly one ordinary space, use text.split(' '). To handle repeated spaces, tabs, and other whitespace, use text.trim().split(/\s+/), while considering whether empty fields at the edges matter.

How do I split a string into characters?

text.split('') divides the string into UTF-16 code units and may break some Unicode symbols. Use Array.from(text) or [...text] to iterate by code point; use grapheme-aware processing if combined visible characters must remain intact.

Why does split() leave empty strings in my array?

Empty entries occur when separators are next to each other or at the start or end of the string. For example, two adjacent commas represent an empty field between them. Remove empty entries only if they are not meaningful in your data.

Can split() parse CSV?

Not reliably for general CSV. A comma can appear inside a quoted field, and fields can include line breaks or escaped quotation marks. Use a CSV parser for files or input that follows the CSV format.

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.