HTML tutorial
CSS3 tutorial
Bootstrap tutorial
JavaScript tutorial
JQuery tutorial
AngularJS tutorial
React tutorial
NodeJS tutorial
PHP tutorial
Python tutorial
Python3 tutorial
Django tutorial
Linux tutorial
Docker tutorial
Ruby tutorial
Java tutorial
C tutorial
C ++ tutorial
Perl tutorial
JSP tutorial
Lua tutorial
Scala tutorial
Go tutorial
ASP.NET tutorial
C # tutorial
Loop through a block of code, but skip the value of 3
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:
The continue statement can also be used with an optional label reference.
Using the optional label reference:
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>";
}
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>";
}
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>";
}
}
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
continue
is an ECMAScript1 (ES1) feature.
ES1 (JavaScript 1997) is fully supported in all browsers:
Chrome | IE | Edge | Firefox | Safari | Opera |
Yes | Yes | Yes | Yes | Yes | Yes |