String.prototype.split()
Convert a string into an array by splitting it after a specific character (or characters).
The first argument, the delimiter
, the character or characters to split by. As an optional second argument, you can stop splitting your string after a certain number of delimiter matches have been found.
let shoppingList = 'Soda, turkey sandwiches, potato chips, chocolate chip cookies';
let menu = shoppingList.split(', ');
let limitedMenu = shoppingList.split(', ', 2);
// logs ["Soda", "turkey sandwiches", "potato chips", "chocolate chip cookies"]
console.log(menu);
// logs ["Soda", "turkey sandwiches"]
console.log(limitedMenu);
⏰ Preorder my new course on Web Components! Want to learn how to build Web Components from scratch, master best practices, and more? Preorder today and get $100 off of the launch price.
Find this useful? You can support my work by purchasing an annual membership.