JavaScript continue

JavaScript continue statement used inside the loops, continue statement skip the current iteration and execute to the next iterations of the loop.

General Syntax

continue [ label ];

Arguments

  1. continue: Required, continue keyword skip the current iteration of the loop.
  2. label: Optional, Control transfer to specified labeled name.

Syntax

while () {
    continue;                   // skip the current iteration
}
for (;;){
    while () {
        continue;               // skip the current iteration
    }
}

Nested loop inside loop skip the current loop and control transfer to specified labelled block.

outer: {
    inner: {
        for(;;){
            while (){                                     
                continue inner;     // skip the current iteration and control transfer to inner { .. }
            }
        }                                       
    }                               // end inner
}                                   // end outer

JavaScript continue (while Loop)

Example In this example we used continue, it would skip current iteration and go to the next iteration of for loop.

<script>
    var i = 0;
    while(i < 10){
        i++;
        if (i == 5){
            document.writeln("skipped, i = 5");
            continue;
        }
        document.writeln(i);
    }
</script>

Run it...   »

JavaScript continue (do..while Loop)

Example

<script>
    var i = 0;
    do {
        i++;
        if (i == 5){
            document.writeln("skipped, i = 5");
            continue;
        }
        document.writeln(i);
    } while(i < 10)
</script>

Run it...   »

JavaScript continue (for Loop)

Example

<script>
    for(var i = 0; i <= 10; i++){
        if (i == 5){
            document.writeln("skipped, i = 5");
            continue;
        }
        document.writeln(i);
    }
</script>

Run it...   »

JavaScript continue with labeled block

Example Following example continue with labeled to skip the for loop and execution control would go to the inner label to execute the next iteration of inner loop.

<script>
for(var i = 1; i < 5; i++){
    inner:
    for (var j = 0; j < 5; j++){
        if (j == 2){
            document.writeln("skipped");
            continue inner;
        }
        document.writeln("i : " + i + ", j :" + j);
    }
    document.writeln();
}
</script>

Run it...   »

Example Result