JavaScript The break and continue Statements
The break statement is used to jump out of a loop.
continue is used to skip one iteration in a loop.
The break Statement
We have already seen the break statement in an earlier chapter of this tutorial. It was used to jump out of a switch() statement.
The break statement can also be used to jump out of a loop.
After the break statement jumps out of the loop, it continues to execute the code after the loop (if any):
Example
Try it Yourself »
Since this if statement has only one line of code, the curly braces can be omitted:
The continue Statement
The continue statementIt breaks the current iteration in the loop, and then continues with the next iteration in the loop. In the following example, when the value is 3, it directly skips it:
For Loop Example
Try it Yourself »
While Loop Example
Try it Yourself »
JavaScript Labels
As you saw in the chapter on switch statements, you can label JavaScript statements.
To label a JavaScript statement, precede it with a label and a colon:
The break and continue statements are the only statements that can jump out of a code block.
Syntax:
The continue statement (with or without a label reference) can only be used inside loops.
The break statement (without a label reference) can only be used inside loops or switch.
With a label reference, the break statement can be used to jump out of any JavaScript code block:
Example
Try it Yourself »