JavaScript Continue Statement

This is a preview lesson
Register or sign in to take this lesson.

In this lesson, we will learn about the javascript continue statement and how to use it. Continue statements terminate the execution of statements in the current iteration of the current or labeled loop and continue execution of the loop with the next iteration. In contrast to a break statement, the continue statement continues the loop’s execution after the current iteration has ended.

Syntax,

continue [label];

Here, the label is optional which denotes a label name of the statement. The labeled continue statement is rarely used in javascript.

Example 1: Continue in while loop

let i = 1;
while ( i  < = 5 ) {
     if (i == 3) {
         continue;
     }
console.log("Value of i: " , i ) ;
i ++;
}

In the above example, the continue statement will skip the while loop when the value of i is equals to 3 and continues the remaining loop. The output will be as shown below:

Value of i: 1
Value of i: 2
Value of i: 4
Value of i: 5

Example 2: Continue in for loop

for (let i = 1; i <= 5; i++) {
    if (i == 3) {
        continue;
    }
   console.log("Value of i: " , i ) ;
}

In the above example, the for loop logs the value of i until its value reaches 5. The continue statement will skip the for a loop when the value of i is equals to 3 and continues the remaining loop. The output will be as shown below:

Value of i: 1
Value of i: 2
Value of i: 4
Value of i: 5

Example 3: Labelled continue

Label1:
for (let i = 0; i < 3; i++) {
   console.log("Value of i: " , i ) ;
Label2:       
  for (let j = 1; j < 3; j++) {
    if (j === 2) continue Label2;
        console.log("Value of j: " , j ) ;
  }
}

In the above example, The continue statement skips the current iteration in the inner loop and executes the console statement in the inner loop.

Output

Value of i:  0
Value of j:  1
Value of j:  3
Value of i:  1
Value of j:  1
Value of j:  3
Value of i:  2
Value of j:  1
Value of j:  3

Conclusion

In this article, we learn about the javascript continue statement and how to use it with the help of some examples. To learn more about javascript visit here.