In JavaScript, you have complete authority over the management of loop statements. Occasionally, you may encounter scenarios where you need to bypass certain code within the loop and proceed to the subsequent iteration. This can be accomplished by utilizing the continue statement provided by JavaScript.
In JavaScript, the continue statement is utilized to skip the current iteration of a loop. In contrast to the break statement, which terminates the loop altogether, the continue statement interrupts the ongoing iteration and proceeds with the next iteration of the loop. This statement can be applied in for loops, while loops, and do-while loops. When employed within a while loop, it returns to the condition check. Conversely, when used in a for loop, the execution flow advances to the update expression.
Upon utilizing the continue statement, the execution flow of the program promptly shifts to the conditional expression. If this condition evaluates to true, the subsequent iteration will commence; if not, the control will exit the loop.
Syntax
continue;
OR
continue[label]; // Using the label reference
It may be utilized with or without the label reference. A label serves as an identifier for a specific statement and is not mandatory.
Let us delve into the concept of the continue statement by examining a few illustrative examples.
Example1
In this illustration, we demonstrate the use of the continue statement within a for loop. The loop starts its iterations from 1 and concludes at 7. A condition is evaluated to determine when the loop's current iteration equals 4. Upon reaching the value of 4, the continue statement is invoked, which causes the loop to bypass the current iteration and proceed directly to the subsequent one.
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<h1> Example of the continue statement in JavaScript</h1>
<h3> Here, you can see that "a == 4" is skipped. </h3>
<p id = "para">
</p>
<script>
var res = "";
var a;
for (a = 1; a <=7; a++) {
if (a == 4) {
continue;
}
res += "The value of a is : " + a + "<br>";
}
document.getElementById("para").innerHTML = res;
</script>
</body>
</html>
Output
Upon running the code provided above, the resulting output will be -
Example2
In this illustration, we are employing the continue statement within a while loop. We start by defining an array named 'rainbow'. The loop commences its iteration from 0 and continues until it reaches the length of the array. A conditional statement is implemented utilizing the OR (||) operator to determine when the iteration encounters the values 'Magenta' and 'Skyblue'. Upon reaching these specified values, the iteration is bypassed using the continue statement, allowing the loop to proceed to the subsequent iteration.
<!DOCTYPE html>
<html>
<head>
<title> JavaScript Continue Statement </title>
</head>
<body>
<h1> Example of the JavaScript Continue Statement </h1>
<h3> You can see that the arrray values "Magenta" and "Skyblue" are skipped. </h3>
<script>
var rainbow = ["Violet", "Indigo", "Magenta", "Blue", "Skyblue", "Green", "Yellow", "Orange", "Red"];
var i = 0;
var res = "";
while(i < rainbow.length){
if (rainbow[i] == "Magenta" || rainbow[i] == "Skyblue") {
i++;
continue;
}
res = "";
res += rainbow[i] + "<br>";
i++;
document.write(res);
}
</script>
</body>
</html>
Output
Upon completion of the aforementioned process, the resulting output will be -
Example3
In this illustration, we demonstrate the use of a label in conjunction with the continue statement. We have a nested for loop where the outer loop is designated with the label 'label1,' while the inner loop is marked with the label 'label2.'
<!DOCTYPE html>
<html>
<body>
<p> This is an example of the continue statement with the label </p>
<p id="para"></p>
<script>
var res = "";
var i, j;
label1: // This loop is labeled as "label1"
for (i = 1; i <=5; i++) {
res += "<br>" + "i = " + i + ", j = ";
label2: // This loop is labeled as "label2"
for (j = 1; j <= 4; j++) {
if (j == 2) {
continue label2;
}
document.getElementById("para").innerHTML = res += j + " ";
}
}
</script>
</body>
</html>
Output
After the execution, the output is -