4.9 Break and Continue

Every loop you have seen so far supports two special keywords that change its behaviour: break and continue. The demonstration uses a while loop, but the same rules apply to for, do...while and any other loop in this section.

Start with a counter that prints numbers from 0 to 10:

let i = 0;
while (i <= 10) {
  console.log(i);
  i++;
}

Sometimes you want to leave a loop earlier because of a condition discovered at runtime. The break keyword stops the loop immediately. If we add if (i === 5) break; inside the body, the console only shows 0 to 4 — as soon as i reaches 5, the loop ends.

break vs continue

  • break — exits the loop completely.
  • continue — skips the rest of the current iteration and jumps to the next one.

With continue we can, for example, print only odd numbers. We check if (i % 2 === 0) { i++; continue; }. When i is 2, we increment it and skip back to the top, so the console.log after that block never runs for even values. That is why we see only odd numbers on the console.

In practice continue is not used very often — it is one of the older constructs inherited from older languages and many teams prefer rewriting the condition explicitly. Use break when you really need to exit a loop early, and remember that continue just jumps to the next iteration.

Summary

This lesson covers two fundamental loop control keywords: `break` and `continue`. The `break` keyword allows you to exit a loop prematurely when a condition is met, while `continue` skips the current iteration and jumps to the next one. Both keywords are demonstrated through practical examples using while loops and conditional statements.

Key points

  • `break` statement immediately terminates the loop when a condition is satisfied (e.g., exiting at i === 5 in a 0-10 loop)
  • `continue` statement skips the remaining code in the current iteration and proceeds to the next loop cycle
  • Both keywords work with all loop types (while, for, do-while) and are controlled by conditional statements like `if`
  • Using `continue` to filter values (e.g., displaying only odd numbers by skipping even ones with modulo operation)
  • `continue` is discouraged in modern JavaScript as it's a legacy feature from older programming paradigms

FAQ

What is the difference between `break` and `continue` in a loop?

`break` exits the entire loop immediately, while `continue` only skips the current iteration and proceeds with the next one. For example, `break` at i=5 exits the loop completely, whereas `continue` allows the loop to continue but skips certain iterations based on conditions.

How can `continue` be used to filter values in a loop?

You can use `continue` with a conditional check like `if (i % 2 === 0) { i++; continue; }` to skip even numbers and only process odd numbers. When the condition is true, `continue` jumps to the next iteration without executing the code that follows.

Is it recommended to use `continue` in modern JavaScript?

No, the lesson notes that `continue` is a legacy feature inherited from older programming paradigms and is not recommended for modern code. It's better to structure your loops with clear conditional logic rather than relying on `continue` to control flow.