Skip to the content

for...of

JavaScript · Unit 3: Loops

for...of goes through a text letter by letter, with no counter and no condition.

On each round, the variable holds the next letter.

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. Predict the output

    What does this code print?

  3. 3. Multiple choice

    How many rounds does for (const c of "book") run?

  4. 4. Complete the code

    Complete the loop header to print each letter of sun.

  5. 5. Predict the output

    What does this code print?

  6. 6. Predict the output

    What does this code print?

  7. 7. Find the bug

    This program should build the text s-u-n-, with a dash after each letter. Which line has the error?

  8. 8. Put the lines in order

    Put the lines in order to count how many a or e letters the word lemonade has.

  9. 9. Multiple choice

    What does this code do?

  10. 10. Write the code

    Write countA(text), which returns how many a letters the text has. For example, countA("banana") gives 3.

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 →