break and continue: Controlling Loop ExecutionJavaScript break stops a loop (or switch) immediately and jumps to the first statement after it. The continue keyword skips the rest of the current iteration and moves straight to the next loop pass.
break and continue WorkUsing break avoids wasted CPU work once you find what you searched for. Using continue keeps loop bodies flat by skipping invalid or ignored items early at the top of each pass.
Note where each keyword jumps: in a for loop, continue still runs the i++ step expression before re-checking the condition.
break, continue, and Labels ExampleLabeled statements (outer: for (...)) let break outer; exit two nested loops at once, though extracting a helper function with return is often even cleaner.
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;
}
}
}
break or continue inside .forEach(): Array callbacks are functions, not loops, so break inside .forEach() throws a SyntaxError. Switch to for...of or .some().continue statements: Labels can make control flow jump unpredictably; prefer moving nested loops into a small helper function and using return.continue in while: Always increment your while counter before continue executes.break exit an if block?Only if that if block sits inside a loop or switch, and break exits the enclosing loop or switch—never a standalone if. You can break out of a labeled block { ... }, though that pattern is rare.
break and return inside a loop?break exits only the loop and continues running code below the loop inside the same function. return exits the entire function immediately.
continue skip the increment step in a for loop?No, in for (let i = 0; i < 5; i++), continue runs i++ and then evaluates i < 5 for the next pass.
Sign in to track your learning journey, earn industry-recognized certificates, and join our elite developer community.
Quick Access With
Experiment with the code from this lesson in our interactive playground.