JavaScript Break and Continue
The break and continue statements control loop flow. break exits a loop entirely, while continue skips the current iteration and moves to the next. These statements provide fine-grained control over loop execution.
Break Statement
The break statement immediately exits the current loop, regardless of the loop's condition. Execution jumps to the first statement after the loop. No more iterations execute—break terminates the loop completely. This provides an early exit when continuing would be pointless or incorrect.
Once break executes, the loop is done—no more iterations occur, and any remaining loop iterations are skipped. If break is inside nested loops, it only exits the innermost loop containing the break statement. To exit multiple nested loops, you'd need labeled breaks or restructured logic.
break can be used in for loops, while loops, do...while loops, and switch statements. In loops, it exits the loop. In switch, it prevents fall-through. break is one of the most important control flow statements for escaping loops when a condition is met.
After break executes, control passes immediately to the statement after the loop. The loop condition isn't re-evaluated—break bypasses it entirely. This makes break perfect for search operations where you can stop as soon as you find what you're looking for.
break is useful to stop a loop when a specific condition is met, even if the loop's main condition is still true. Common uses: finding a value in an array (stop when found), detecting invalid input (stop on error), or implementing early termination in algorithms (stop when answer is found).
// Break in for loop
for (let i = 0; i < 10; i++) {
if (i === 5) {
break; // Exit loop when i is 5
}
console.log(i); // 0, 1, 2, 3, 4
}
// Break in while loop
let count = 0;
while (true) {
console.log(count);
count++;
if (count >= 5) {
break; // Exit infinite loop
}
}
// Search and break
const numbers = [1, 2, 3, 4, 5];
let found = false;
for (let num of numbers) {
if (num === 3) {
found = true;
break;
}
}
Continue Statement
The continue statement skips the rest of the current iteration and immediately moves to the next iteration of the loop. It doesn't exit the loop—just the current pass through it. Control jumps back to the loop's condition check (for while) or increment (for for loops), then starts the next iteration if the condition allows.
After continue executes, the loop continues to its next iteration. The loop condition is re-evaluated, and if still true, another iteration begins. continue lets you selectively skip certain iterations while allowing the loop to continue normally for others. It's perfect for filtering during iteration.
Any code after the continue statement in the current iteration is skipped—it won't execute for that iteration. However, unlike break, the loop itself continues. Subsequent iterations proceed normally. Only the current iteration is affected by continue.
continue is useful for skipping specific values or conditions without terminating the entire loop. Common uses: skipping negative numbers in calculations, ignoring invalid data while processing remaining items, or filtering during iteration without creating a new array.
The key difference from break: continue skips to the next iteration while break exits the loop entirely. continue says "skip this one, try the next"; break says "we're done, exit the loop." Use continue when you want to selectively process items; use break when you want to stop processing entirely.
// Continue in for loop
for (let i = 0; i < 10; i++) {
if (i % 2 === 0) {
continue; // Skip even numbers
}
console.log(i); // 1, 3, 5, 7, 9
}
// Skip negative numbers
const numbers = [1, -2, 3, -4, 5];
for (let num of numbers) {
if (num < 0) {
continue; // Skip negative
}
console.log(num); // 1, 3, 5
}
// While loop with continue
let i = 0;
while (i < 10) {
i++;
if (i === 5) {
continue;
}
console.log(i); // All except 5
}