JavaScript continue Statement
JavaScript statement reference manual
Example
In this example we used the continue statement in a loop.
Loop through the code block, skipping the current iteration when the value of i equals "3":
var text = ""
var i;
for (i = 0; i < 5; i++) {
if (i == 3) {
continue;
}
text += "The number is " + i + "
";
}
text The output is:
The number is 0
The number is 1
The number is 2
The number is 4
Try it »
The bottom of this article contains more examples.
Definition and Usage
continue is used to skip one iteration in a loop and continue execution with the next iteration of the loop.
continue and break The difference is that break ends the entire loop body, while continue ends a single iteration.
However, when executing the continue statement, two different types of loops are exhibited:
- in while In the loop, the condition is evaluated first; if the condition is true, the loop runs once more.
- in for In the loop, the increment expression (e.g. i++) is evaluated first, then the condition is checked to see if it is true, and then it is decided whether to execute the iteration.
The continue statement can be applied to an optional label reference.
Note: The continue statement (without a label reference) can only be used in loops or switch statements.
Browser support
| Statement | |||||
|---|---|---|---|---|---|
| continue | Yes | Yes | Yes | Yes | Yes |
Syntax
continue;
Use an optional label reference:
continue
labelname
;
Technical Details
| JavaScript version: | 1.0. JavaScript 1.2 supports optional label references. |
|---|
More examples
Example
In this example we used the continue statement in a while loop.
Loop through the code block, skipping the current iteration when i equals "3":
var text = "";
var i = 0;
while (i < 5) {
i++;
if (i == 3) {
continue;
}
text += "
The number is " + i;
}
text The output is:
The number is 1
The number is 2
The number is 4
The number is 5
Try it »
Example
In this example we used the continue statement in a for loop.
Loop through an array, skipping the current iteration when the array element is "Saab":
var cars = ["BMW", "Volvo", "Saab", "Ford"];
var text = ""
var i;
for (i = 0; i < cars.length; i++) {
if (cars[i] == "Saab") {
continue;
}
text += cars[i] + "
";
}
text The output is:
BMW
Volvo
Ford
Try it »
Example
Use the continue statement with a label reference to break out of a code block:
var text = "";
var i, j;
Loop1: // First loop label "Loop1"
for (i = 0; i < 3; i++) {
text += "
" + "i = " + i + ", j = ";
Loop2: // Second loop label "Loop2"
for (j = 10; j < 15; j++) {
if (j == 12) {
continue Loop2;
}
document.getElementById("demo").innerHTML = text += j + " ";
}
}
text The output is:
i = 0, j = 10 11 13 14
i = 1, j = 10 11 13 14
i = 2, j = 10 11 13 14
Try it »
Related pages
JavaScript tutorial: JavaScript Break and Continue
JavaScript tutorial: JavaScript loops
JavaScript tutorial: JavaScript While Loop
JavaScript tutorial: JavaScript break statement
JavaScript Reference Manual: JavaScript for Statement
JavaScript Reference Manual: JavaScript while Statement
JavaScript statement reference manual