Skip to the content

Looping over text

JavaScript · Unit 6: Strings

A for...of goes through a string letter by letter, just like it goes through an array.

Spaces and punctuation get their own round too.

for (const letter of "sun") {
  console.log(letter);
}

Prints

s
u
n

The rest of the explanation is in the lesson, which is part of the full plan.

Exercises in this lesson

You do them in the app, which checks them on the spot and explains why.

  1. 1. Predict the output

    What does this code print?

  2. 2. Complete the code

    Complete it so it prints "ccaatt".

  3. 3. Complete the code

    Complete the function that counts the vowels in a string.

  4. 4. Predict the output

    What does this code print?

  5. 5. Find the case that fails

    countA(text) should count how many a the text has, uppercase or lowercase. Which call breaks it?

  6. 6. Predict the output

    What does this code print?

  7. 7. Find the bug

    This program should remove the spaces and print "helloworld". Which line has the error?

  8. 8. Multiple choice

    What does this function do?

  9. 9. Put the lines in order

    Order the function that separates the letters with a space and turns them uppercase ("hello" gives "H E L L O").

  10. 10. Predict the output

    What does this code print?

Do this lesson

It opens in your browser. This lesson is part of the full plan; the first unit of each course is free.

See all lessons →