JavaScript continue
Examples
Loop through a block of code, but skip the value of 3:
let text = "";
for (let i = 0; i < 5; i++) {
if (i === 3) continue;
text += i + "<br>";
}
Try it Yourself »
let text = "";
let i = 0;
while (i < 5) {
i++;
if (i === 3) continue;
text += i + "<br>";
}
Try it Yourself »
More examples below.
Description
The continue
statement breaks one iteration (in the loop) if a specified
condition occurs, and continues with the next iteration in the loop.
The difference between continue and the break statement, is instead of "jumping out" of a loop, the continue statement "jumps over" one iteration in the loop.
However, when the continue statement is executed, it behaves differently for different types of loops:
- In a while loop, the condition is tested, and if it is true, the loop is executed again
- In a for loop, the increment expression (e.g. i++) is first evaluated, and then the condition is tested to find out if another iteration should be done
The continue statement can also be used with an optional label reference.
Note: The continue statement (with or without a label reference) can only be used inside a loop.
Syntax
continue;
Using the optional label reference:
continue labelname;
More Examples
Skip the numbers 2 and 3 (using the OR operator):
let text = "";
for (let i = 1; i < 8; i++) {
if (i === 2 || i === 3) continue;
text += i + ""<br>";
}
Try it Yourself »
Loop over an array, but skip "Saab":
const cars = ["BMW", "Volvo", "Saab", "Ford"];
let text = "";
for (let = 0; i < cars.length; i++) {
if (cars[i] === "Saab") {
continue;
}
text += cars[i] + "<br>";
}
Try it Yourself »
Example
With a label reference, skip a value in a nested loop:
let text = "";
// The first for loop is labeled Loop1:
Loop1:
for (let i = 0; i < 3; i++) {
text += i + "<br>";
// The second for loop is labeled Loop2:
Loop2:
for (let i = 10; i < 15; i++) {
if (i === 12) continue Loop2;
text += i + "<br>";
}
}
Try it Yourself »
Related Pages
JavaScript Tutorial: JavaScript Break and Continue
JavaScript Tutorial: JavaScript For Loop
JavaScript Tutorial: JavaScript While Loop
JavaScript Tutorial: JavaScript break Statement
JavaScript Reference: JavaScript for Statement
JavaScript Reference: JavaScript while Statement
Browser Support
continue
is an ECMAScript1 (JavaScriopt 1997) feature.
It is supported in all browsers:
Chrome | Edge | Firefox | Safari | Opera | IE |
Yes | Yes | Yes | Yes | Yes | Yes |