JavaScript String split()
Examples
Split the words:
const myArray = text.split(" ");
Split the words, and return the second word:
const myArray = text.split(" ");
let word = myArray[1];
Split the characters, including spaces:
Use the limit parameter:
More examples below.
Description
The split() method splits a string into an array of substrings.
The split() method returns the new array.
The split() method does not change the original string.
If (" ") is used as separator, the string is split between words.
Syntax
Parameters
A string or regular expression to use for splitting.
If omitted, an array with the original string is returned.
An integer that limits the number of splits.
Items after the limit are excluded.
Return Value
More Examples
Split a string into characters and return the second character:
Use a letter as a separator:
If the separator parameter is omitted, an array with the original string is returned:
Browser Support
split() is an ECMAScript1 (JavaScript 1997) feature.
It is supported in all browsers:
| Chrome | Edge | Firefox | Safari | Opera |