.split()
Breaks the string on which it called into an array of substrings, and returns the new array.
Syntax
string.split(separator);
The separator
(optional) describes the pattern where each split should occur. It may be a string or a regular expression. If not provided, the returned array will contain the entire string as its lone element.
string.split(separator, limit);
The limit
(optional) determines the number of substring elements included in the array.
Examples
Split string into array of names:
const stringOfPeople = 'Dominic, Shelly, Luka, Devin';const arrayOfPeople = stringOfPeople.split(', ');console.log(arrayOfPeople);// Output: ['Dominic', 'Shelly, 'Luka, 'Devin']
Limit the number of names in array to three:
const stringOfPeople = 'Dominic, Shelly, Luka, Devin';const arrayOfPeople = stringOfPeople.split(', ', 3);console.log(arrayOfPeople);// Output: ['Dominic', 'Shelly, 'Luka']
Split string without providing a separator argument:
const stringOfPeople = 'Dominic, Shelly, Luka, Devin';const arrayOfPeople = stringOfPeople.split();console.log(arrayOfPeople);// Output: ['Dominic, Shelly, Luka, Devin']
Split string on each character:
const letters = 'abcde';const lettersArray = letters.split('');console.log(lettersArray);// Output: ['a', 'b', 'c', 'd', 'e']