Break & Continue

In JavaScript loops, break and continue are control statements that alter the normal flow of iteration.

Illustration of break continue
Illustration of break continue

💡 What do they do?

  • break: Immediately exits the entire loop.
  • continue: Skips the current iteration and moves to the next one.

How break works

The break statement stops the loop entirely when executed.

📌 Deep Dive: Using break

JAVASCRIPT
for (let i = 1; i <= 5; i++) {
  if (i === 3) {
    break; // Exit loop when i equals 3
  }
  console.log(i);
}
Output
1
2

How continue works

The continue statement skips the rest of the current loop iteration and proceeds with the next iteration.

📌 Deep Dive: Using continue

JAVASCRIPT
for (let i = 1; i <= 5; i++) {
  if (i === 3) {
    continue; // Skip the rest when i equals 3
  }
  console.log(i);
}
Output
1
2
4
5
Comparison: break vs continue
Featurebreakcontinue
Effect on LoopTerminates loop completelySkips current iteration only
Next stepExit loopProceed to next iteration
Common use caseStop loop when condition metIgnore certain values or steps

⚠️ Important Note

Using break or continue outside loops will cause syntax errors. They only work inside for, while, or do...while loops.

Summary

  • break stops and exits the entire loop immediately.
  • continue skips the rest of the current loop iteration and moves on.
  • Both help control loop execution flow based on conditions.