Javascript - Statement continue statement


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

Here's an example:


var num = 0;

for (var i=1; i < 10; i++) {
    if (i % 5 == 0) {
        continue;//w w w  .j a v  a2  s  .com

console.log(num);    //8


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