JS Break
JavaScript break and continue: Controlling Loop Execution
The JavaScript break statement stops a loop (or a switch) immediately and jumps to the first statement after it. The continue statement skips the rest of the current iteration and moves straight to the next one.
This guide shows how each keyword behaves, how labels let you exit nested loops, and the mistakes that most often cause confusing bugs.
How break and continue Work in JavaScript
Use break when you have what you came for. Once you find a match, there is no reason to keep looping. Use continue to skip items you don't care about, so the main logic stays flat instead of buried inside an if block.
Think of searching a box of files: continue means "not this one, next file," and break means "found it, stop searching."
Where continue jumps depends on the loop type:
for loop: the i++ update expression still runs, then the condition is checked.
while and do...while loops: there is no automatic update, so continue jumps straight to the condition check. If your counter changes at the bottom of the body, continue will skip it. See our lesson on JavaScript while loops.
Practical break, continue, and Label Example
const records = [
{ id: 1, active: false, name: "Skip Me" },
{ id: 2, active: true, name: "Target User" },
{ id: 3, active: true, name: "Later User" },
];
for (const record of records) {
if (!record.active) {
continue; // Skip inactive records
}
console.log("First active user:", record.name);
break; // Stop searching after first active match
}
// Labeled break on nested grid search
outerLoop: for (let row = 0; row < 3; row++) {
for (let col = 0; col < 3; col++) {
if (row === 1 && col === 1) {
console.log("Found center cell (1,1), stopping search.");
break outerLoop;
}
}
}
Our lesson on JavaScript functions covers return in more detail.
Common Mistakes to Avoid
Trying
breakorcontinueinside.forEach(): Array callbacks are functions, not loops, sobreakinside.forEach()throws aSyntaxError. Switch tofor...ofor.some().Overusing labeled
continuestatements: Labels can make control flow jump unpredictably; prefer moving nested loops into a small helper function and usingreturn.Skipping counter updates with
continueinwhile: Always increment yourwhilecounter beforecontinueexecutes.
Frequently Asked Questions
Can break exit an if block?
Not on its own. A bare break is only valid inside a loop or switch, and it exits that enclosing loop or switch, not the if. You can break out of a labeled block (label: { ... }), but that pattern is rare.
What is the difference between break and return inside a loop?
break exits only the loop, and the code below it in the same function keeps running. return exits the whole function immediately.
Does continue skip the increment step in a for loop?
No. In for (let i = 0; i < 5; i++), continue still runs i++ and then checks i < 5.
Can I use break inside a switch statement?
Yes. In a switch, break stops execution from falling through into the next case. Without it, matching cases keep running until a break or the end of the switch.
Does continue work in a for...of or for...in loop?
Yes. It skips to the next item in the sequence, just as it does in a regular for loop.
Conclusion
Use break to stop once you have your answer and continue to skip items early. Be careful with while loops, avoid break in .forEach(), and treat labels as a last resort in favor of a small function with return. Next, review JavaScript for loops
Unlock Your
Full Potential.
Sign in to track your learning journey, earn industry-recognized certificates, and join our elite developer community.
Quick Access With
Try it Yourself
Experiment with the code from this lesson in our interactive playground.