JavaScriptStrings

String.prototype.split()

Splits a string into an array of pieces based on a separator: a character, a string, or a regular expression.

Returns
A new array of strings.
Changes the original
No
On this page

Example

#
JavaScript
const tags = 'html,css,javascript'
const sentence = 'I love coding every day'

console.log(tags.split(','))
console.log(sentence.split(' '))
console.log(tags.split(',', 2))
Console
['html', 'css', 'javascript']
['I', 'love', 'coding', 'every', 'day']
['html', 'css']

Definition and usage

#

The split() method looks for a separator in a string and cuts the text wherever it finds one. The pieces land in a new array, and the separator itself disappears. It's the basic way to turn text, such as a comma-separated list of tags or a sentence, into an array you can easily process with array methods.

The separator can be a string or a regular expression, such as /\s+/ for any number of spaces. An empty string "" splits the text into individual UTF-16 units, which breaks emoji apart, so use [...text] instead to split into characters. Without a separator, you get an array with the whole string as its only element.

The second argument, limit, caps the number of elements in the result. When the separator sits at the start, at the end, or twice in a row, empty strings show up in the array. The reverse operation is join() on an array.

Syntax

#
Syntax
string.split(separator)
string.split(separator, limit)

Parameters

#
  • separator

    string | RegExp, optional

    A string or regular expression marking where to split. If omitted, you get an array with a single element: the whole string.
  • limit

    number, optional

    The maximum number of elements in the result. The rest of the text is discarded.

More examples

#
Irregular spacing
JavaScript
const input = '  React,   Vue ,Angular  '

const frameworks = input
  .split(',')
  .map((name) => name.trim())

console.log(frameworks)
console.log('one  two   three'.split(/\s+/))
Console
['React', 'Vue', 'Angular']
['one', 'two', 'three']
Empty elements and splitting into characters
JavaScript
console.log('a,,b,'.split(','))
console.log('web'.split(''))
console.log('Hey👋'.split('').length, [...'Hey👋'].length)
Console
['a', '', 'b', '']
['w', 'e', 'b']
5 4

Empty strings appear between two commas and after the last one. split("") cuts emoji in half, so it is safer to use [...text] to split text into characters.

Parsing simple data
JavaScript
const row = 'Emma;120;JavaScript'
const [name, points, course] = row.split(';')

console.log(name, Number(points) + 30, course)

const lines = 'line 1\nline 2\nline 3'.split('\n')
console.log(lines.length)
Console
Emma 150 JavaScript
3

Browser support

#

Widely available

Works in all modern browsers, phones included. You can use it without worry.

  • Chrome
  • Edge
  • Firefox
  • Safari

Good practices

#
  • After split(), it is often handy to map((part) => part.trim()), because data typed by humans tends to have stray spaces.
  • Numbers after a split are still strings. Convert them with Number() before doing any math, otherwise "120" + 30 gives "12030".
  • To split into characters, use [...text] or Array.from(text). split("") breaks emoji into incorrect halves.

Related entries

#

Spotted a mistake or missing an example? Write to us.