Skip to the content

for

JavaScript · Unit 3: Loops

for repeats a block. Its three parts go inside the parentheses, separated by ;: where it starts, how long it keeps going and how it moves.

i++ adds 1 to i at the end of each round. What repeats is whatever is inside the {}.

for (let i = 0; i < 3; i++) {
  console.log("Hello");
}

Prints

Hello
Hello
Hello

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 times does the block of for (let i = 0; i < 5; i++) repeat?

  4. 4. Complete the code

    Complete this to print the numbers from 1 to 10.

  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 the countdown so it prints 5, 4, 3, 2, 1 and lifts off.

  8. 8. Find the bug

    This program should print the whole 7 times table: 7, 14, 21 and on up to 70. Which line has the error?

  9. 9. Put the lines in order

    Put the lines in order to print the even numbers from 1 to 6.

  10. 10. Multiple choice

    What does this code do?

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 →