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?

KeywordWhat it doesReal-life example
breakStops the loop completely and exitsStop searching once the keys are found
continueSkips the current round and goes to the nextSkip 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

ii === 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

ii === 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:

Pointbreakcontinue
What happensLoop ends completelyOnly the current round is skipped
Next stepJumps to the code after the loopJumps to the next round
Rounds after it❌ None run✅ They still run
Works in switch?✅ Yes❌ No
Real-life exampleFound the keys, stop searchingSkip 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 after continue.


⚠️ 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

MistakeProblemFix
continue before the update in whileInfinite loopUpdate the variable before continue
Using break or continue in forEach()❌ SyntaxErrorUse for...of
Expecting break to exit all nested loopsOnly the inner loop stopsUse labels or a flag variable
Using continue in switch❌ Not allowed outside a loopUse break in switch
Using too many break/continueCode becomes hard to followKeep conditions simple and clear

Key Takeaways

  • break stops the loop completely and jumps to the code after it.
  • continue skips only the current round and moves to the next one.
  • Use break to stop early (like when searching) and continue to skip unwanted items (like invalid data).
  • In nested loops, both affect only the loop they are written in.
  • Be careful with continue in while loops (it can cause infinite loops), and remember that neither works in forEach().