JavaScript Break and Continue
The break and continue statements give you control over loop execution. break exits the loop entirely, while continue skips the current iteration and moves to the next one.
The break Statement
The break statement terminates the current loop immediately. Execution continues with the first statement after the loop.
// Exit loop when value is found
for (let i = 0; i < 10; i++) {
if (i === 5) {
console.log("Found 5, stopping!");
break;
}
console.log(i);
}
// Output: 0, 1, 2, 3, 4, "Found 5, stopping!"
// Search in array
let numbers = [10, 20, 30, 40, 50];
let target = 30;
let found = false;
for (let i = 0; i < numbers.length; i++) {
if (numbers[i] === target) {
found = true;
console.log("Found " + target + " at index " + i);
break;
}
}
// Output: "Found 30 at index 2"The continue Statement
The continue statement skips the rest of the current iteration and jumps to the next one. The loop continues running.
// Skip even numbers
for (let i = 0; i < 10; i++) {
if (i % 2 === 0) {
continue; // Skip even numbers
}
console.log(i);
}
// Output: 1, 3, 5, 7, 9
// Skip specific items
let fruits = ["apple", "", "banana", null, "cherry"];
for (let fruit of fruits) {
if (!fruit) {
continue; // Skip empty/null values
}
console.log(fruit);
}
// Output: apple, banana, cherrybreak vs continue
The key difference: break exits the entire loop, while continue only skips the current iteration. Choose based on whether you want to stop or skip.
| Feature | break | continue |
|---|---|---|
| Effect | Exits the loop entirely | Skips to next iteration |
| Code after it | Not executed (exits loop) | Not executed (this iteration) |
| Loop continues? | No | Yes |
| Use case | Stop searching after finding | Skip unwanted items |
console.log("--- Using break ---");
for (let i = 1; i <= 5; i++) {
if (i === 3) break;
console.log(i);
}
// Output: 1, 2
console.log("--- Using continue ---");
for (let i = 1; i <= 5; i++) {
if (i === 3) continue;
console.log(i);
}
// Output: 1, 2, 4, 5Labeled Statements
Labels allow you to break or continue outer loops from inside nested loops. A label is an identifier followed by a colon before a loop statement.
// Labeled break: exit outer loop
outer: for (let i = 0; i < 3; i++) {
for (let j = 0; j < 3; j++) {
if (i === 1 && j === 1) {
console.log("Breaking outer loop");
break outer;
}
console.log("i=" + i + ", j=" + j);
}
}
// Output: i=0,j=0 i=0,j=1 i=0,j=2 i=1,j=0 Breaking outer loop
// Labeled continue: skip to next outer iteration
outer2: for (let i = 0; i < 3; i++) {
for (let j = 0; j < 3; j++) {
if (j === 1) {
continue outer2;
}
console.log("i=" + i + ", j=" + j);
}
}
// Output: i=0,j=0 i=1,j=0 i=2,j=0break in Nested Loops
Without labels, break only exits the innermost loop. To exit multiple levels, use labels or restructure your code using functions with return.
// Without label: break only exits inner loop
for (let i = 0; i < 3; i++) {
for (let j = 0; j < 3; j++) {
if (j === 1) break; // Only breaks inner loop
console.log("i=" + i + ", j=" + j);
}
}
// Output: i=0,j=0 i=1,j=0 i=2,j=0
// Alternative: use a function with return
function findValue(matrix, target) {
for (let i = 0; i < matrix.length; i++) {
for (let j = 0; j < matrix[i].length; j++) {
if (matrix[i][j] === target) {
return { row: i, col: j }; // Exits both loops
}
}
}
return null;
}
let grid = [[1, 2], [3, 4], [5, 6]];
console.log(findValue(grid, 4)); // { row: 1, col: 1 }