JavaScript Break and Continue
- Previous Page JS Loop While
- Next Page JS Iterable Objects
break
Statement 'break out' of the loop.
continue
Statement 'skip' an iteration in the loop.
Break statement
In the earlier chapters of this tutorial, you have seen break
statement. It is used to 'break out' of switch
statement.
The break statement can also be used to break out of a loop.
break statement
It will break out of the loop and continue executing the code after the loop (if any):
Example
for (i = 0; i < 10; i++) { if (i === 3) { break; } text += "Number is " + i + "<br>"; }
Continue statement
continue statement
Break (in a loop) an iteration if a specified condition occurs. Then continue with the next iteration in the loop.
This example skips the value 3 :
Example
for (i = 0; i < 10; i++) { if (i === 3) { continue; } text += "Number is " + i + "<br>"; }
JavaScript label
To mark a JavaScript statement, please place the label name and colon before the statement:
label: statements
break
and continue
The statement is the only JavaScript statement that can be used to 'break out' of a code block.
Syntax:
break labelname; continue labelname;
continue
statement (whether with or without label reference) can only be used forSkipping over an iteration.
break
statement, if no label reference is used, can only be used forBreak out of a loop or a switch.
If there is a label reference, then break
statements can be usedBreak out of any code block:
Example
var cars = ["BMW", "Volvo", "Saab", "Ford"]; list: { text += cars[0] + "<br>"; text += cars[1] + "<br>"; text += cars[2] + "<br>"; break list; text += cars[3] + "<br>"; text += cars[4] + "<br>"; text += cars[5] + "<br>"; }
Code blocks refer to {
With }
direct code snippets.
Supplementary Books
For more information on JavaScript Break and Continue Statementsfor more information on the knowledge, please read the relevant content in the Advanced JavaScript Tutorial:
- ECMAScript break and continue Statements
- This section explains the differences between break statements and continue statements, and how to use them with labeled statements.
- Previous Page JS Loop While
- Next Page JS Iterable Objects