JavaScript continueStatement

JavaScript 语句参考手册JavaScript Statement Reference

Examples

In this example, we used the continue statement in a loop.

Loop through a code block, skipping the current iteration when i is equal to '3':

var text = ""
var i;
for (i = 0; i < 5; i++) {
    if (i == 3) {
        continue;
    }
    text += "The number is " + i + "<br>";
}

textThe output is:

The number is 0
The number is 1
The number is 2
The number is 4

Try it Yourself »

More examples are included at the bottom of this article.


Definition and Usage

The continue statement skips one iteration in a loop and continues with the next iteration in the loop.

continue andbreakThe difference between the statements is that break ends the entire loop, while continue ends the current iteration.

However, when executing the continue statement, two different types of loops behave differently:

  • InwhileIn a loop, the condition is checked first; if the condition is true, the loop executes again.
  • InforIn a loop, the increment expression (e.g., i++) is evaluated first, then the condition is checked to determine whether to execute the iteration.

The continue statement can be used with an optional label reference.

Note:The continue statement (without a label reference) can only be used in a loop or in a switch.


Browser Support

Statement
continue Yes Yes Yes Yes Yes


Syntax

continue;

Using an optional label reference:

continue labelname;

Technical Details

JavaScript Version: 1.0. JavaScript 1.2 supports optional label references.


实例

More Examples

Examples

In this example, we used the continue statement in a while loop.

Loop through a code block, skipping the current iteration when i is equal to '3':

var text = "";
var i = 0;
while (i < 5) {
    i++;
    if (i == 3) {
        continue;
    }
text += "<br>The number is " + i;
}

textThe output is:

The number is 1
The number is 2
The number is 4
The number is 5

Try it Yourself »

Examples

In this example, we used the continue statement in a for loop.

Loop through an array, skipping the current iteration when the array element is 'Saab':

var cars = ["BMW", "Volvo", "Saab", "Ford"];
var text = ""
var i;
for (i = 0; i < cars.length; i++) {
    if (cars[i] == "Saab") {
        continue;
    }
    text += cars[i] + "<br>";
}

textThe output is:

BMW
Volvo
Ford

Try it Yourself »

Examples

Use the continue statement with a label reference to break out of a code block:

var text = "";
var i, j;

Loop1: // the first loop label 'Loop1'
for (i = 0; i < 3; i++) {
text += "<br>" + "i = " + i + ", j = ";

Loop2: // the second loop label 'Loop2'
    for (j = 10; j < 15; j++) {
        if (j == 12) {
            continue Loop2;
        }
        document.getElementById("demo").innerHTML = text += j + " ";
    }
}

textThe output is:

i = 0, j = 10 11 13 14
i = 1, j = 10 11 13 14
i = 2, j = 10 11 13 14

Try it Yourself »


Related Pages

JavaScript Tutorial:JavaScript Break and Continue

JavaScript Tutorial:JavaScript Loop

JavaScript Tutorial:JavaScript While Loop

JavaScript Tutorial:JavaScript break Statement

JavaScript Reference:JavaScript for Statement

JavaScript Reference:JavaScript while Statement


JavaScript 语句参考手册JavaScript Statement Reference Other Extensions