Back to Javascript
Breaking and continuing loops
In JavaScript, you can control the flow of loops using break and continue statements
In JavaScript, you can control the flow of loops using break and continue statements:
breakstatement:Exits the loop entirely.
Stops executing any further iterations in a loop.
continuestatement:Skips the current iteration and continues with the next one.
The loop does not terminate, but moves to the next iteration.
Here's a breakdown with examples:
break Statement Example
In this example:
When
ireaches5, thebreakstatement stops the loop.
continue Statement Example
In this example:
The
continuestatement skips even numbers and moves to the next iteration wheniis even.
Both statements can also be used in while and do...while loops to control the loop flow in the same way.