JavaScript ES2024 ·
✓ verified by execution on 2026-08-10
When you write loops, you sometimes need more control over how they run. Instead of letting a loop finish every single iteration, you might want to stop it early when you find what you’re looking for, or skip an iteration if it doesn’t match your criteria. JavaScript provides two statements for this: break and continue.
The path of control flow: break escapes the loop entirely, while continue jumps to the next iteration.
Exiting entirely: the break statement
The break statement terminates the current loop completely. When JavaScript encounters break, it immediately stops running the loop and moves on to the code that comes after the loop.
This is extremely useful when searching for something. Once you find it, there is no need to keep checking the rest of the items!
javascript · visualize
let count = 0;for (let i = 1; i <= 5; i++) { if (i === 3) { break; // Stop the loop entirely when i is 3 } count++;}// Control jumps here after the breakconsole.log("Final count:", count);
Skipping an iteration: the continue statement
The continue statement is different. It doesn’t destroy the loop; it just terminates the current iteration. When JavaScript sees continue, it skips the rest of the loop body for that iteration and jumps straight to the next iteration.
javascript · visualize
let count = 0;for (let i = 1; i <= 5; i++) { if (i === 3) { continue; // Skip the rest of this iteration } count++;}console.log("Final count:", count);
[!TIP]
In a for loop, using continue still evaluates the update expression (like i++). The loop counter progresses as normal!
Let’s test your understanding of this. What do you think the following code will print?
Predict the outputjavascript
Read the code. What exactly will it print? Commit to an answer before you look.
for (let i = 0; i < 5; i++) { if (i === 2) { continue; } console.log(i);}
Output
0
1
3
4
You predicted
The while loop trap
There is a major gotcha when using continue inside a while loop. Because while loops don’t have a built-in update expression like for loops, you usually manually update your counter inside the loop body.
If you use continuebefore updating your counter, you will create an infinite loop!
javascript
let i = 0;let iters = 0;try { while (i < 3) { if (i === 1) { if (iters++ > 10) throw new Error('Infinite loop detected!'); // DANGER: We forgot to increment 'i' before continuing! continue; } i++; }} catch (e) { console.log(e.message);}
Output
Infinite loop detected!
Your output
To fix this, you must ensure that your loop condition variable is updated before the continue statement is executed.
Labels and Nested Loops
By default, break and continue only affect the innermost loop they are inside. If you have a loop inside another loop (nested loops), a break in the inner loop will not stop the outer loop.
javascript
let loopBreaks = 0;for (let i = 0; i < 3; i++) { switch (i) { case 1: // This break ONLY exits the switch, not the for loop! break; } loopBreaks++;}console.log("The loop ran", loopBreaks, "times.");
Output
The loop ran 3 times.
Your output
If you really need to break out of an outer loop from an inner loop, you can use a label. A label is an identifier followed by a colon (:) placed before a loop. You can then reference this label in your break or continue statement.
A labelled break escapes not just the inner loop, but the specific labelled outer loop.
javascript
let found = false;outerLoop: for (let i = 0; i < 3; i++) { for (let j = 0; j < 3; j++) { if (i === 1 && j === 1) { found = true; break outerLoop; // Escapes BOTH loops! } }}console.log("Found:", found);
Output
Found: true
Your output
Using labels is rare, but they are a powerful tool when you are deeply nested and need an immediate escape hatch.
Practice
Check yourself
What happens when a `break` statement is executed inside a loop?
Reveal answer
The loop immediately terminates and execution continues after the loop. — The `break` statement terminates the current loop entirely, moving control to the statement following the loop.
Why can using `continue` inside a `while` loop be dangerous?
Reveal answer
It can create an infinite loop if the counter is not updated before the `continue`. — If you use `continue` in a `while` loop before updating the condition variable (like `i++`), the loop will run forever because the update step is skipped.
How do you break out of an outer loop from inside an inner loop?
Reveal answer
Use a labeled statement (e.g., `break outerLoop;`). — You can use a label (e.g., `outer:`) on the outer loop and then use `break outer;` inside the inner loop.
Challenges
Challenge 1 +20 XP
Print the first even number in the array using a break statement.
javascript
Test 1 — expects "4"
Need a hint? (−25% XP)
Iterate through the array. Use the modulo operator (`% 2 === 0`) to check for even numbers. When found, assign it to `firstEven` and `break`.