In JavaScript, you can split a string into an array of substrings using split():
s.split(delim) // splits on a delimiter string
s.split(regex) // splits on a regular expression
s.split("") // splits into individual characters
Here are some examples:
let sentence = "hello world foo";
let words = sentence.split(" ");
console.log(words); // ["hello", "world", "foo"]
let csv = "a,b,c";
let parts = csv.split(",");
console.log(parts); // ["a", "b", "c"]
You can loop through the resulting array:
words.forEach(w => console.log(w));
Assignment
Follow the Coding Tutorial and split sentence into an array of words!
Hint
Use sentence.split(" ") and assign the result to words.
Free plan: 30 tutor messages. PRO is unlimited.
Saved
Ctrl + Enter to check
