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
Example
#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))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
#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
#const input = ' React, Vue ,Angular '
const frameworks = input
.split(',')
.map((name) => name.trim())
console.log(frameworks)
console.log('one two three'.split(/\s+/))console.log('a,,b,'.split(','))
console.log('web'.split(''))
console.log('Hey👋'.split('').length, [...'Hey👋'].length)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.
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)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 tomap((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" + 30gives"12030". - To split into characters, use
[...text]orArray.from(text).split("")breaks emoji into incorrect halves.
Related entries
#- Array.prototype.join()Joins the array’s elements into a single string with a chosen separator.
- String.prototype.slice()Returns a portion of a string from a start index to an end index (not included). Supports negative indexes.
- String.prototype.trim()Removes whitespace from both ends of a string: spaces, tabs, and newline characters.
- String.prototype.match()Matches a string against a regular expression. Returns an array with the match or all matches, or null.
- RegExpA regular expression: a pattern for searching, validating, and replacing pieces of text.
- DestructuringDestructuring pulls values out of arrays and objects straight into separate variables.
Spotted a mistake or missing an example? Write to us.