JavaScript Break and Continue
The break
and continue
statements are used in JavaScript to alter the flow of a for
, while
, or do...while
loop.
For example, if you use the break
statement within a for
loop, the loop will be terminated early.
If you use the continue
statement within a for
loop, one iteration of the loop will be skipped.
The Break Statement
The break
keyword is used to immediately terminate a loop. When the break
keyword is encountered in a loop, the loop will be exited and control will be passed to the next statement following the loop.
Here is an example of using the break
keyword in a for
loop:
Editor
In this example:
- The loop will be terminated when
i
is equal to 3.
The Continue Statement
The continue
keyword is used to skip the current iteration of a loop and move on to the next iteration.
When the continue
keyword is encountered in a loop, the current iteration will be skipped and control will be passed to the next iteration of the loop.
Here is an example of using the continue
keyword in a for loop:
Editor
In this example:
- The iteration when
i
is equal to 3 will be skipped.