Skip to the content

Looping

JavaScript · Unit 5: Arrays

for...of walks through an array: on each pass, the variable takes the next item. You need no indexes and no length.

const fruits = ["mango", "pear"];
for (const f of fruits) {
  console.log("Today:", f);
}

Prints

Today: mango
Today: pear

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. Find the bug

    This program should print the sum of the scores. Which line has the error?

  3. 3. Predict the output

    What does this code print?

  4. 4. Complete the code

    Complete it to print every name.

  5. 5. Predict the output

    What does this code print?

  6. 6. Predict the output

    What does this code print?

  7. 7. Complete the code

    Complete it so Ava can get into the party.

  8. 8. Find the case that fails

    largest(nums) should return the biggest number in the array. It never gets an empty array. Which call makes it fail?

  9. 9. Put the lines in order

    Put the lines in order to print the total of the costs with forEach.

  10. 10. Predict the output

    These are the sales for each day. What does it 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 →