Start learning free

LESSON 68 OF 671 · STRINGS I

String Split in JavaScript

Lesson

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.

STEP 1 OF 1
    Ctrl + Enter to check