Academic Block

JavaScript Break and Continue Get Certified

The break and continue statements give you extra control over how a loop runs. They let you exit a loop early or skip specific iterations without needing extra conditions wrapped around the entire loop body.

In this lesson, you will learn how break and continue work, how to use labels to control nested loops, and how to avoid some common mistakes.

The break Statement

The break statement immediately exits the loop it is inside, skipping any remaining iterations entirely.

What this code does: break exits the loop immediately. When i reaches 4, the loop stops, so only 1, 2, and 3 are printed, then “Loop ended.”

for (let i = 1; i <= 10; i++) {
    if (i === 4) {
        break;
    }
    console.log(i);
}
console.log("Loop ended.");

Once i reaches 4, the loop stops completely — the remaining values (5 through 10) are never checked.

break in a while Loop

break works the same way in while and do...while loops.

What this code does: A common pattern: an intentional infinite while (true) that stops with break when a condition is met. Here it prints 1, 2, 3 then exits.

let i = 0;
while (true) {
    i++;
    if (i > 3) {
        break;
    }
    console.log(i);
}

Here, while (true) would normally run forever, but break gives us a controlled way to exit once a condition is met.

break in a switch Statement

break is also used inside a switch statement to stop it from falling through into the next case.

What this code does: In a switch, break prevents fall-through to the next case. Matching “green” prints “Go!” and then leaves the switch.

let color = "green";
switch (color) {
    case "red":
        console.log("Stop!");
        break;
    case "green":
        console.log("Go!");
        break;
    default:
        console.log("Unknown color.");
}

The continue Statement

The continue statement skips the rest of the current iteration and jumps straight to the next one. Unlike break, it does not stop the loop entirely.

What this code does: continue skips the rest of the current iteration. Multiples of 3 are skipped, so 1, 2, 4, 5, 7, 8, 10 are printed.

for (let i = 1; i <= 10; i++) {
    if (i % 3 === 0) {
        continue;
    }
    console.log(i);
}

This loop skips every multiple of 3, but continues checking the remaining numbers all the way up to 10.

continue in a while Loop

When using continue in a while loop, be careful that the update expression still runs before continue, or the loop may never end.

What this code does: In a while loop, continue jumps back to the condition check. Even numbers are skipped after incrementing, so only the odd numbers 1, 3, 5, 7, 9 are printed.

let i = 0;
while (i < 10) {
    i++;
    if (i % 2 === 0) {
        continue;
    }
    console.log(i);
}

Because i++ runs before the continue check, the loop variable is always updated, avoiding an infinite loop.

break vs. continue

Aspect break continue
Effect on the loop Exits the loop entirely Skips to the next iteration
Remaining iterations Never run Still run, except the skipped one
Also usable in switch? Yes No (only in loops)

Labeled Statements

By default, break and continue only affect the loop they are directly inside. A label lets you target an outer loop from within a nested loop.

A label is written as a name followed by a colon, placed directly before the loop it identifies.

What this code does: A labeled break can exit an outer loop from inside a nested loop. When j === 2, break outerLoop stops both loops at once. Only “i=1, j=1” is printed.

outerLoop:
for (let i = 1; i <= 3; i++) {
    for (let j = 1; j <= 3; j++) {
        if (j === 2) {
            break outerLoop;
        }
        console.log("i=" + i + ", j=" + j);
    }
}

Without the label, break would only stop the inner loop, and the outer loop would continue running. With break outerLoop;, both loops stop immediately.

Labeled continue

A label can also be used with continue to skip to the next iteration of an outer loop instead of the inner one.

What this code does: A labeled continue skips to the next iteration of the outer loop. When j === 2, the rest of that row is skipped and the outer loop moves to the next i.

outerLoop:
for (let i = 1; i <= 3; i++) {
    for (let j = 1; j <= 3; j++) {
        if (j === 2) {
            continue outerLoop;
        }
        console.log("i=" + i + ", j=" + j);
    }
}

Here, as soon as j reaches 2, the inner loop is abandoned and control jumps straight to the next iteration of the outer loop, incrementing i.

Practical Example

Let’s use break and continue together to search for a specific value in a nested list, stopping as soon as it’s found.

What this code does: A practical search in a 2D grid. When the target value is found, a labeled break exits both loops immediately instead of continuing to scan the rest of the grid.

const grid = [
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9]
];
const target = 6;
let found = false;
search:
for (let row = 0; row < grid.length; row++) {
    for (let col = 0; col < grid[row].length; col++) {
        if (grid[row][col] === target) {
            found = true;
            console.log("Found " + target + " at row " + row + ", col " + col);
            break search;
        }
    }
}
if (!found) {
    console.log(target + " was not found in the grid.");
}

Common Mistakes With break and continue

  • Expecting break inside a nested loop to stop every loop, when it only stops the innermost one by default.
  • Placing continue before the loop’s update expression in a while loop, accidentally creating an infinite loop.
  • Overusing labels for simple cases where restructuring the loop logic would be clearer.
  • Forgetting that continue cannot be used inside a switch statement on its own — it always refers to an enclosing loop.
  • Using break or continue outside of a loop (or switch, for break), which causes a syntax error.

Best Practices for break and continue

  • Use break to stop searching once you’ve found what you’re looking for, instead of letting the loop run unnecessarily.
  • Use continue to skip irrelevant values instead of wrapping the rest of the loop body in an if statement.
  • Reserve labeled statements for genuinely nested loop scenarios, and give labels clear, descriptive names.
  • Double-check that continue in a while loop doesn’t skip a necessary update expression.
  • Consider extracting complex nested loops into a separate function, which can use return instead of a labeled break.

Quick Summary

  • break exits a loop (or switch) immediately.
  • continue skips the rest of the current iteration and moves to the next one.
  • Both statements work in for, while, and do...while loops.
  • Labels let break and continue target an outer loop from within a nested loop.
  • A label is written as labelName: directly before the loop it identifies.
  • Using these statements thoughtfully can make loops more efficient and easier to read.

Try It Yourself

Modify the code below. Try changing which value triggers break or continue, and see how the output changes.

What this code does: Practice example combining both: even numbers are skipped with continue, and the loop stops at 7 with break. Change the values and click Test My Code to experiment.

for (let i = 1; i <= 10; i++) {
    if (i === 7) {
        break;
    }
    if (i % 2 === 0) {
        continue;
    }
    console.log(i);
}
Tip: Think of break as “I’m done here, stop everything,” and continue as “skip this one, but keep going.” Keeping that distinction clear in your head makes it much easier to predict how a loop will behave.

Understanding break and continue is useful before moving on to functions, arrays, and more advanced JavaScript concepts.

🧪 Test Your JavaScript Code

Edit the code on the left and click “Run Code” to see the console output on the right.

📝 JavaScript Code
👁 Console Output

Click “Run Code” to see the output here.

Ctrl + Enter to run code Esc to close