Javascript - Statement continue statement

Introduction

The continue statement exits the current loop iteration and continues from the top of the loop.

Here's an example:

Demo

var num = 0;

for (var i=1; i < 10; i++) {
    if (i % 5 == 0) {
        continue;/*from w ww.j  a  va 2s.  c  om*/
    }
    num++;
 }

console.log(num);    //8

Result

Here, the console.log displays 8, the number of times the loop has been executed.

When i reaches a value of 5, the loop is jumped back to loop start before num is incremented.

The execution continues with the next iteration, when the value is 6.

The loop continues until its natural completion, when i is 10.

The final value of num is 8 instead of 9, because one increment didn't occur because of the continue statement.

Related Topics

Exercise