![]() |
VOOZH | about |
JavaScript break statement is used to terminate the execution of the loop or the switch statement when the condition is true.
Mango is delicious.
Why do we need to use break with Switch?
In the below example, the fruit name is apple but the given output is for the two cases. This is because of the break statement. In the case of Apple, we are not using a break statement which means the block will run for the next case also till the break statement not appear.
Apple is healthy. Mango is delicious.
1 2 3
How does Break Work?
1 2 3 1 2 3
How does it Work?
In JavaScript, we can use a break statement with a label to exit from a specific loop, even if it's nested inside another loop. This is useful when you need to break out of a nested loop structure, and just using a simple break would only exit the innermost loop.
i: 0, j: 0 i: 0, j: 1 i: 0, j: 2 i: 1, j: 0