Skip to the content

What a for...of can loop over

JavaScript · Unit 16: Iterators and generators

A for...of is not just for arrays. It works with anything that knows how to hand over its values one at a time: text, a Set and a Map too.

That's what being iterable means.

const people = ["ann", "leo"];
const seen = new Set(people);
for (const n of seen) {
  console.log(n);
}

Prints

ann
leo

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

    Fill in the blank to go over the text letter by letter.

  3. 3. Predict the output

    What does this code print?

  4. 4. Complete the code

    Fill in the blank to add up the week's kilometres.

  5. 5. Multiple choice

    Which of these CAN'T be looped over with a for...of?

  6. 6. Predict the output

    What does this code print?

  7. 7. Find the bug

    It should print the number and then the day, like this: 0. Mon. Which line has the bug?

  8. 8. Predict the output

    How many different letters does the word have?

  9. 9. Put the lines in order

    Build numbered(xs): it returns an array with each item after its position, like "0. bread".

  10. 10. Multiple choice

    What ends up stored in missing?

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 →