Skip to the content

while

JavaScript · Unit 3: Loops

while repeats the block as long as its condition is true.

It checks it before each round; when it stops being true, it carries on with what comes after.

let n = 1;
while (n <= 3) {
  console.log(n);
  n++;
}
console.log("Done");

Prints

1
2
3
Done

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. Multiple choice

    How many times does this code print something?

  3. 3. Predict the output

    What does this code print?

  4. 4. Multiple choice

    What would happen if you deleted the line lives--;?

  5. 5. Complete the code

    Maya saves 30 a week. Complete this to count how many weeks it takes her to save at least 100.

  6. 6. Find the bug

    This program should print the powers of 2 up to 64: 1, 2, 4 and on up to 64. Which line has the error?

  7. 7. Predict the output

    The person types 5, then 3 and then 0. What does it print?

  8. 8. Put the lines in order

    Put the lines in order to show the phone battery while there is charge left. Each hour uses 30.

  9. 9. Predict the output

    What does this code print?

  10. 10. Multiple choice

    If the person types 2026, 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 →