JavaScript split – The split() method takes a pattern as input and divides a given String into an ordered list of substrings by finding the pattern, putting these substrings into an array, and returning the array. It does not change the original string. If (” “) is used as the separator, the string is split between the words.
SYNTAX – str.split(separator, upto)
separator – A string or any expression which is used for splitting. It is optional. If in any case omitted, it will return an array of original strings.
- upto – It is a non-negative integer that decides the number of splits. Items after upto are excluded. An array may contain lesser entries than upto if the end of the string is reached before the limit is reached. If upto is 0 return [].
For example –
Split a string into characters
const arr = str.split(“”);
Use any letter as a separator:
const arr = str.split(“o”);
If the separator parameter is omitted/not given
const arr = str.split();