Skip to content

JavaScript Break

The break and continue statements are control flow statements that alter the execution of loops (for, while, do...while) and switch statements.

The break statement terminates the current loop or switch statement entirely.

The continue statement skips the current iteration of a loop and proceeds to the next iteration.

You’ve likely encountered break in switch statements to prevent fall-through. It’s also commonly used to exit a loop prematurely when a certain condition is met.

When break is executed inside a loop, the loop immediately terminates, and program execution continues with the statement following the loop.

Example: Finding an element and stopping.

let numbers = [10, 20, 30, 40, 50];
let foundAt = -1;
let target = 30;
let output = "";
for (let i = 0; i < numbers.length; i++) {
output += `Checking ${numbers[i]}\n`;
if (numbers[i] === target) {
foundAt = i;
output += `Found ${target} at index ${i}. Breaking loop.\n`;
break; // Exit the loop once the target is found
}
}
output += `Loop finished. Target found at index: ${foundAt}`;
// console.log(output) would show that checking stops after 30.

Without break, the loop would continue checking all numbers even after finding the target.

The continue statement is used inside loops to skip the remainder of the current iteration and proceed to the next one.

Example: Processing only even numbers.

let result = "";
for (let i = 0; i < 10; i++) {
if (i % 2 !== 0) { // If the number is odd
continue; // Skip the rest of this iteration
}
result += `Processing even number: ${i}\n`;
}
// console.log(result) would show only even numbers processed.

In this example, if i is odd, continue is executed, and the result += ... line is skipped for that iteration. The loop then proceeds with the next value of i.

Common Learning Obstacle: break vs. continue

Remember: break exits the entire loop. continue only exits the current iteration.

JavaScript allows you to label statements. A label is an identifier followed by a colon (:), placed before a statement or a block of code.

labelName: statementOrBlock

break and continue can be used with labels to control execution flow in nested loops or specific code blocks. This is a less common feature but can be useful in certain scenarios.

Syntax with labels:

break labelName;
continue labelName;

A break labelName; statement will terminate the execution of the code block or loop associated with labelName.

Example: Breaking out of a nested loop.

let outputText = "";
outerLoop: // Label for the outer loop
for (let i = 0; i < 3; i++) {
outputText += `Outer loop (i=${i}):\n`;
for (let j = 0; j < 3; j++) {
outputText += ` Inner loop (j=${j})\n`;
if (i === 1 && j === 1) {
outputText += ` Breaking out of outerLoop!\n`;
break outerLoop; // Exits both inner and outer loops
}
}
}
outputText += "After loops.";
// console.log(outputText) would show loops stopped after i=1, j=1.

A continue labelName; statement will skip the current iteration of the labeled loop and proceed to the next iteration of that specific labeled loop.

Example: Continuing an outer loop from an inner loop.

let log = "";
outer:
for (let i = 0; i < 3; i++) {
log += `Outer i=${i}\n`;
for (let j = 0; j < 3; j++) {
if (j === 1) {
log += ` Inner j=${j}, continuing outer\n`;
continue outer; // Skips rest of inner, goes to next iteration of outer
}
log += ` Inner j=${j}\n`;
}
}
// console.log(log) would show inner loop only processes j=0 for each i.

Consideration for Labels:

While labels provide powerful control, they can sometimes make code harder to read and debug. Use them judiciously. Often, refactoring the code into smaller functions can be a more readable alternative to complex labeled break or continue statements.

A code block is a sequence of statements enclosed in curly braces {}.

Note: break and continue work with traditional for, for...of, for...in, while, and do...while loops. They do not directly break or continue array methods like forEach(). For forEach(), you might return from the callback to achieve a continue-like effect for the current item, but you cannot break the forEach() loop itself (you’d need to use Array.prototype.some() or Array.prototype.every(), or switch to a traditional loop).