What a for...of can loop over
JavaScript · Unit 16: Iterators and generators
- Full plan
- JavaScript
- 10 exercises
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. Predict the output
What does this code print?
2. Complete the code
Fill in the blank to go over the text letter by letter.
3. Predict the output
What does this code print?
4. Complete the code
Fill in the blank to add up the week's kilometres.
5. Multiple choice
Which of these CAN'T be looped over with a
for...of?6. Predict the output
What does this code print?
7. Find the bug
It should print the number and then the day, like this:
0. Mon. Which line has the bug?8. Predict the output
How many different letters does the word have?
9. Put the lines in order
Build
numbered(xs): it returns an array with each item after its position, like"0. bread".10. Multiple choice
What ends up stored in
missing?
It opens in your browser. This lesson is part of the full plan; the first unit of each course is free.