var shoppingList = 'apples,pears,cookies,bread'; // Returns ["apples", "pears", "cookies", "bread"] var shoppingListArray = shoppingList.split(',');
Here we can see that the split method, split the entire string based on the given separator and gives us an array of splitted strings.
Separator – It is the value by which we want to split our string. If the separator is unspecified then the entire string becomes one single array element.
The same also happens when the separator is not present in the string. If the separator is an empty string (“”) then every character of the string is separated.
Limit – Limit refers to the upper limit on the number of splits to found in the given string.
const names = 'Harry,John,Clark,Peter,Rohn,Alice'; const nameArr = names.split(','); console.log(nameArr); // output ['Harry', 'John', 'Clark', 'Peter', 'Rohn', 'Alice']