![]() |
VOOZH | about |
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 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.
break;Using break in a for loop
0 1 2 3 4 Breaking the loop at 5
In this example
Using break in a while Loop
1 2 3 4 5 Loop stopped at 6
In this example
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
1 3 5 7 9
In this example
Using continue in a while Loop
1 2 4 5 7 8 10
In this example
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
0 0
In this example
Using continue with Labels
0 0 1 0 2 0
In this example
The break, continue, and label statements are powerful tools for controlling loop execution in JavaScript.