Break & Continue
Imagine you are searching for your house keys. 🔑
You check the table, then the sofa, then your bag, and there they are! Do you keep searching the kitchen and the bedroom? Of course not. You stop searching right away.
Now imagine you are eating a thali. 🍽️ You eat each item one by one, but you skip the karela (bitter gourd) because you don't like it, and then continue eating the rest.
Loops in JavaScript normally run every round until the condition becomes false. But sometimes we want to:
- Stop the loop early, like finding the keys → use
break - Skip one round and move to the next, like skipping the karela → use
continue
What Are Break and Continue?
| Keyword | What it does | Real-life example |
|---|---|---|
break | Stops the loop completely and exits | Stop searching once the keys are found |
continue | Skips the current round and goes to the next | Skip the karela, continue eating the rest |
Both work in all loops: for, while, do-while, for...of, and for...in.
Part 1: The break Statement
break immediately stops the loop. The remaining rounds are skipped, and JavaScript jumps to the code after the loop.
Syntax
for (...) {
if (condition) {
break; // exit the loop right now
}
}
Simple Example
How It Works
i | i === 5? | Action |
|---|---|---|
| 1 | ❌ | Print 1 |
| 2 | ❌ | Print 2 |
| 3 | ❌ | Print 3 |
| 4 | ❌ | Print 4 |
| 5 | ✅ | break → exit the loop |
| 6–10 | — | Never reached |
Even though the loop was set to run until 10, break stopped it at 5.
Real Examples with break
Example 1: Searching for a name in a list 🔍
JavaScript didn't waste time checking Sneha and Vikas. This makes your program faster!
Example 2: Stop shopping when the budget ends 💰
Example 3: break with while (true)
A common pattern is to run a loop "forever" and use break to exit when a goal is reached.
Example 4: break in a switch
You already saw break in the Switch Statement lesson. There, it stops JavaScript from falling through to the next case.
Part 2: The continue Statement
continue skips the rest of the current round and jumps straight to the next round of the loop. The loop does not stop.
Syntax
for (...) {
if (condition) {
continue; // skip this round
}
// this code is skipped when continue runs
}
Simple Example
How It Works
i | i === 3? | Action |
|---|---|---|
| 1 | ❌ | Print 1 |
| 2 | ❌ | Print 2 |
| 3 | ✅ | continue → skip printing, go to the next round |
| 4 | ❌ | Print 4 |
| 5 | ❌ | Print 5 |
Only 3 was skipped. The loop kept going after that.
Real Examples with continue
Example 1: Printing only odd numbers
Example 2: Skipping absent students 📋
Example 3: Ignoring invalid data
Example 4: Skipping the karela 🍽️
Break vs Continue: Side by Side
The same loop, with just one word changed:
| Point | break | continue |
|---|---|---|
| What happens | Loop ends completely | Only the current round is skipped |
| Next step | Jumps to the code after the loop | Jumps to the next round |
| Rounds after it | ❌ None run | ✅ They still run |
Works in switch? | ✅ Yes | ❌ No |
| Real-life example | Found the keys, stop searching | Skip the karela, keep eating |
Break and Continue in Nested Loops
In nested loops, break and continue affect only the loop they are written in, which is the inner loop.
The outer loop kept running all 3 rows. Only the inner loop stopped at col === 2 each time.
Want to stop the outer loop too? JavaScript has labels for that. We will learn them in the Labeled Statements lesson.
⚠️ Watch Out: continue in While Loops
In a while loop, be careful where you place continue. If it runs before the update, the update gets skipped and you get an infinite loop!
❌ Wrong:
let i = 0;
while (i < 5) {
if (i === 2) {
continue; // skips i++ below → i stays 2 forever!
}
console.log(i);
i++;
}
✅ Correct: update before continue
In a for loop, this problem doesn't happen, because the update (
i++) runs automatically even aftercontinue.
⚠️ They Don't Work in forEach()
break and continue cannot be used inside forEach().
Fix: Use a for...of loop instead:
Common Mistakes to Avoid
| Mistake | Problem | Fix |
|---|---|---|
continue before the update in while | Infinite loop | Update the variable before continue |
Using break or continue in forEach() | ❌ SyntaxError | Use for...of |
Expecting break to exit all nested loops | Only the inner loop stops | Use labels or a flag variable |
Using continue in switch | ❌ Not allowed outside a loop | Use break in switch |
Using too many break/continue | Code becomes hard to follow | Keep conditions simple and clear |
Key Takeaways
breakstops the loop completely and jumps to the code after it.continueskips only the current round and moves to the next one.- Use
breakto stop early (like when searching) andcontinueto skip unwanted items (like invalid data). - In nested loops, both affect only the loop they are written in.
- Be careful with
continuein while loops (it can cause infinite loops), and remember that neither works inforEach().