JavaScript break and continue: Control Loop Progress
Quick answer
break exits the nearest enclosing loop or switch when unlabeled. continue skips the remainder of the current iteration and proceeds according to the enclosing loop’s continuation rules.
How it works
In a for loop, continue still evaluates the update expression before testing again. In a while loop it proceeds to the test, so skipping a manual update may prevent progress. Labels allow control to target an enclosing statement or loop under specific grammar rules; use them sparingly. return instead exits a function call.
for (let i = 0; i < 6; i++) { if (i === 3) { break; } console.log(i); }Expected output in Node.js:
012The loop exits before logging 3.
for (let i = 1; i <= 5; i++) { if (i % 2 === 0) { continue; } console.log(i); }Expected output in Node.js:
135Only even iterations are skipped; later iterations still run.
Common mistakes
Break is not a way to skip only one iteration. In a while loop, make sure continue cannot skip the only state change needed to terminate.
Practice
Sum positive odd integers up to n, skipping even integers.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
JavaScript break and continue: Control Loop Progress — Knowledge Check
Apply the taught concept and reason about the result.
What does break do inside the innermost unlabeled loop?
What does continue skip?
What is printed by for(let i=0;i<3;i++){if(i===1)continue;console.log(i); }?
What risk arises if continue skips a while-loop counter update?
What is required for a labeled continue target?