Summary: in this tutorial, you will learn how to use the JavaScript continue statement to skip the current iteration of a loop.
The continue statement skips the current iteration of a loop and immediately jumps to the next one. Because of this, the continue statement must appear in the body of a loop, or you will get an error.
Similar to the break statement, the continue statement has two forms: labeled and unlabeled. For more information on the label statement, see the break statement tutorial.
Using unlabeled JavaScript continue statement
The unlabeled continue statement skips the current iteration of a for, do...while, or while loop.
The continue statement skips the rest of the code to the end of the innermost body of a loop and evaluates the expression that controls the loop.
In a for loop, the continue skips all the statements underneath it and pass the execution of the code to the update expression; in this case, it is i++;
for (var i = 0; i < count; i++) {
if (condition)
continue; // Jumps to expression: i++
// more statement here
}Code language: JavaScript (javascript)In a while or do-while loop, it jumps back to the expression that controls the loop.
while (expression){ // continue jumps here
if (condition) {
continue; // Jumps to expression
}
// more statements here
// ...
}Code language: JavaScript (javascript)do{
if (condition) {
continue; // Jumps to expression
}
// more statements here
// ...
} while(expression); // continue jumps hereCode language: JavaScript (javascript)See the following example:
let s = 'This is a JavaScript continue statement demo.';
let counter = 0;
for (let i = 0; i < s.length; i++) {
if (s.charAt(i) != 's') {
continue;
}
//
counter++;
}
console.log('The number of s found in the string is ' + counter);Code language: JavaScript (javascript)How the script works.
- First, the
countervariable is set to zero. - Second, the
forloop iterates over a string and counts the occurrences of the letters“. If the current character is nots, thecontinuestatement skips the rest of the loop and examines the next character in the string. In case the current character iss, the script increments thecountervariable. - Third, the
console.logoutputs the number ofscharacter found in the string to the console.
Using JavaScript continue with a label
The continue statement can include an optional label as follows:
continue label;Code language: JavaScript (javascript)The label can be any valid identifier. See the following example:
// continue with a label
outer: for (let i = 1; i <= 3; i++) {
for (let j = 1; j <= 3; j++) {
if ((i == 2) && (j == 2)) {
console.log('continue to outer');
continue outer;
}
console.log("[i:" + i + ",j:" + j + "]");
}
}Code language: JavaScript (javascript)How the script works.
- First, the
forloops increment the variableiandjfrom 1 to 3. - Second, inside the body of the innermost loop, we check if both
iandjare equal to 2. If so, we output a message to the console and jump back to theouterlabel. Otherwise, we display the values ofiandjin each iteration.
The following shows the result of the script in the console window.
[i:1,j:1]
[i:1,j:2]
[i:1,j:3]
[i:2,j:1]
continue to outer
[i:3,j:1]
[i:3,j:2]
[i:3,j:3]Code language: CSS (css)As you see from the output when both i and j are equal to 3, the execution of the code jumps back to the expression i++ in the outer loop.
In this tutorial, you have learned how to use the JavaScript continue statement to skip the current iteration of a loop.