JavaScript Break and Continue
In JavaScript, break and continue are used to control loop execution. break immediately terminates a loop when a condition is met, while continue Skips the current iteration and proceeds to the next loop iteration. Additionally, JavaScript supports labels, which can be used with break and continue to control nested loops efficiently.
The break Statement
The break statement is used to exit a loop when a certain condition is satisfied. It is commonly used when searching for a specific value in an array or when an early exit from a loop is required.
Syntax
break;
Using break in a for loop
for (let i = 0; i < 10; i++) {
if (i === 5) {
console.log("Breaking the loop at", i);
break;
}
console.log(i);
}
Output
0 1 2 3 4 Breaking the loop at 5
In this example
- The loop starts from i = 0 and iterates up to i < 10.
- When i reaches 5, the break statement executes, terminating the loop immediately.
- Numbers 0 to 4 are printed before the loop stops.
Using break in a while Loop
let n = 1;
while (n <= 10) {
if (n === 6) {
console.log("Loop stopped at", n);
break;
}
console.log(n);
n++;
}
Output
1 2 3 4 5 Loop stopped at 6
In this example
- The loop starts from n = 1 and runs as long as n <= 10.
- When n reaches 6, the break statement is executed, stopping the loop.
- The numbers 1 to 5 are printed before the loop terminates.
The Continue statement
The continue statement skips the current iteration of the loop and moves to the next iteration without terminating the loop.
Syntax
continue;
Using continue in a for Loop
for (let i = 0; i < 10; i++) {
if (i % 2 === 0) {
continue; // Skips even numbers
}
console.log(i);
}
Output
1 3 5 7 9
In this example
- The loop iterates from i = 0 to i < 10.
- If i is an even number (i % 2 === 0), the continue statement is executed, skipping that iteration.
- Only odd numbers are printed.
Using continue in a while Loop
let i = 0;
while (i < 10) {
i++;
if (i % 3 === 0) {
continue; // Skips multiples of 3
}
console.log(i);
}
Output
1 2 4 5 7 8 10
In this example
- The loop starts from i = 0 and runs while i < 10.
- If i is a multiple of 3, the continue statement skips that iteration.
- Numbers 3, 6, and 9 are not printed.
JavaScript Labels
Labels in JavaScript provide a way to name a loop, which can then be referenced using break or continue. This is useful when dealing with nested loops, where break or continue needs to be applied to a specific loop.
Syntax
labelName:
statement;
Using break with Labels
outerLoop:
for (let i = 0; i < 3; i++) {
for (let j = 0; j < 3; j++) {
if (j === 1) {
break outerLoop; // Exits both loops
}
console.log(i, j);
}
}
Output
0 0
In this example
- The outerLoop label marks the outer for loop.
- When j === 1, the break outerLoop; statement terminates both inner and outer loops.
Using continue with Labels
outerLoop:
for (let i = 0; i < 3; i++) {
for (let j = 0; j < 3; j++) {
if (j === 1) {
continue outerLoop; // Skips to the next iteration of the outer loop
}
console.log(i, j);
}
}
Output
0 0 1 0 2 0
In this example
- When j === 1, the continue outerLoop; statement skips to the next iteration of the outer loop.
- The inner loop execution stops early, printing only j = 0 for each i.
Conclusion
The break, continue, and label statements are powerful tools for controlling loop execution in JavaScript.
- Use break when you need to exit a loop entirely.
- Use continue when you want to skip specific iterations but continue looping.
- Use labels to control nested loops effectively.