Skip to the content

break and continue

JavaScript · Unit 3: Loops

break leaves the loop right then, even if rounds are left.

The program carries on with whatever comes after the loop.

for (let n = 1; n < 10; n++) {
  if (n === 4) {
    break;
  }
  console.log(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

    What does break do?

  3. 3. Predict the output

    What does this code print?

  4. 4. Complete the code

    Complete this to print 1 to 7, skipping the multiples of 3.

  5. 5. Predict the output

    This code looks for the first number whose square goes past 50. What does it print?

  6. 6. Find the bug

    This program should print only the first multiple of 7 greater than 30. Which line has the error?

  7. 7. Put the lines in order

    Put the lines in order to print the first multiple of 9 greater than 50 and stop there.

  8. 8. Predict the output

    You have to guess the 7. The person types 3, then 9 and then 7. What does it print?

  9. 9. 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 →