← Back to Chapters

JavaScript Break and Continue Statements

? JavaScript Break and Continue Statements

⚡ Quick Overview

In JavaScript loops and switch statements, the break and continue keywords give you fine control over how and when the loop runs.

  • break immediately stops the current loop or switch and exits it.
  • continue skips the current iteration and moves on to the next one.
  • They can also be used with labels to control nested loops.

? Key Concepts

  • Loop control – change the normal flow of for, while, and do...while loops.
  • Immediate exit – use break when you have found what you need and can stop looping early.
  • Skip logic – use continue to skip certain values (e.g., even numbers, invalid input).
  • Nested loops + labels – labels allow you to break or continue a specific outer loop.

? Syntax and Theory

Break syntax:

? break Statement Syntax
break;
break labelName;

Continue syntax:

? continue Statement Syntax
continue;
continue labelName;

When used without a label, break and continue only affect the innermost loop. With a label, they can target an outer loop.

? Code Examples

⛔ Break in a for Loop
for (let i = 0; i < 10; i++) {
if (i === 5) {
break; // Exit loop when i is 5
}
console.log(i);
}
// Output: 0 1 2 3 4
⏭️ Continue in a for Loop
for (let i = 0; i < 10; i++) {
if (i % 2 === 0) {
continue; // Skip even numbers
}
console.log(i);
}
// Output: 1 3 5 7 9
? Break in a while Loop
let count = 0;
while (true) {
console.log("Count is:", count);
count++;
if (count === 3) {
break; // Exit infinite loop when count reaches 3
}
}
// Output:
// Count is: 0
// Count is: 1
// Count is: 2
? Continue to Skip a Specific Value
for (let i = 1; i <= 10; i++) {
if (i === 7) {
continue; // Skip number 7
}
console.log(i);
}
// Output: 1 2 3 4 5 6 8 9 10
? Nested Loops with break
for (let i = 1; i <= 3; i++) {
for (let j = 1; j <= 3; j++) {
if (j === 2) {
break; // Breaks inner loop when j is 2
}
console.log(`i=${i}, j=${j}`);
}
}
// Output:
// i=1, j=1
// i=2, j=1
// i=3, j=1
?️ Using Labels with break
outerLoop:
for (let i = 1; i <= 3; i++) {
for (let j = 1; j <= 3; j++) {
if (i === 2 && j === 2) {
break outerLoop; // Breaks the outer loop
}
console.log(`i=${i}, j=${j}`);
}
}
// Output:
// i=1, j=1
// i=1, j=2
// i=1, j=3
// i=2, j=1

? Live Output and Explanation

? How These Examples Behave

  • In the first for loop, the loop stops completely when i reaches 5, so only 0 to 4 are printed.
  • In the continue example, all even numbers are skipped, so only odd numbers appear in the output.
  • The while (true) loop would be infinite, but break stops it safely once count becomes 3.
  • In the nested loop without labels, break only affects the inner loop, so the outer loop continues with the next value of i.
  • In the labeled example, break outerLoop stops the outer loop entirely as soon as i === 2 and j === 2.

? Common Use Cases

  • Stopping a search once a matching element is found in an array or list.
  • Skipping invalid or incomplete user inputs inside a processing loop.
  • Breaking out of nested loops when a certain condition is met (using labels).
  • Preventing infinite loops by using break when a safe exit condition occurs.

? Tips and Best Practices

  • break exits the entire current loop or switch immediately.
  • continue skips the current iteration but keeps the loop running.
  • Use labels carefully and only when needed, to avoid confusing control flow.
  • Keep loop conditions and updates correct to avoid infinite loops.
  • Avoid overusing break and continue; if logic feels complex, consider refactoring.
  • Comment your loops when using labels so future readers understand the control flow.

? Try It Yourself

  • Write a loop that prints numbers 1 to 10 but breaks the loop if the number is greater than 6.
  • Create a loop that skips all multiples of 3 using continue.
  • Use nested loops and a label to break out of the outer loop from inside the inner loop.
  • Modify one of the examples to use continue with a label and observe how control jumps to the next outer iteration.