JavaScript continue statement
- Previous Page const
- Next Page debugger
- Go Up One Level JavaScript Statement Reference Manual
Definition and usage
If a specified condition occurs, the continue statement will interrupt one iteration (within the loop) and continue to the next iteration within the loop.
The difference between the continue and break statements is that the continue statement does not "exit" the loop, but rather "skips" one iteration within the loop.
However, when the continue statement is executed, its behavior will differ depending on the type of loop:
- In the while loop, test the condition, and if it is true, execute the loop again
- In the for loop, first calculate the increment expression (e.g., i++), then test the condition to determine whether another iteration should be performed
- The continue statement can also be used with optional label references
Note: The continue statement (with or without label reference) can only be used within a loop.
Example
In this example, we use the for loop with the continue statement.
Skip a section of code, but skip the value "3":
var text = "" var i; for (i = 0; i < 5; i++) { if (i === 3) { continue; } text += "The number is " + i + "<br>"; }
More TIY examples are below the page.
Syntax
continue;
Use optional labels for references:
continue labelname;
Technical details
JavaScript version: | ECMAScript 1 |
---|
More examples
Example
In this example, we use the while loop with the continue statement.
Skip a section of code, but skip the value "3":
var text = ""; var i = 0; while (i < 5) { i++; if (i === 3) { continue; } text += "<br>The number is " + i; }
The result of the text will be:
The number is 1 The number is 2 The number is 4 The number is 5
Example
Loop through a block of code but skip the numbers 2 and 3 (using the OR operator):
var text = ""; var i; for (i = 1; i < 8; i++) { if (i === 2 || i === 3) continue; document.getElementById("demo").innerHTML += i + "<br>"; }
The result of the text will be:
1 4 5 6 7
Example
In this example, we use the for loop with the continue statement.
Loop through an array but skip the array element "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>"; }
The result of the text will be:
BMW Volvo Ford
Example
Use the continue statement with a label reference to skip values in nested for loops:
var text = ""; var i, j; Loop1: // The first for loop is labeled "Loop1" for (i = 0; i < 3; i++) { text += "<br>" + "i = " + i + ", j = "; Loop2: // The second for loop is labeled "Loop2" for (j = 10; j < 15; j++) { if (j === 12) { continue Loop2; } document.getElementById("demo").innerHTML = text += j + " "; } }
Browser Support
Statements | Chrome | IE | Firefox | Safari | Opera |
---|---|---|---|---|---|
continue | Support | Support | Support | Support | Support |
Related Pages
JavaScript Tutorial:JavaScript Break and Continue
JavaScript Tutorial:JavaScript For Loop
JavaScript Tutorial:JavaScript While Loop
JavaScript Tutorial:JavaScript break Statement
JavaScript Reference Manual:JavaScript for Statement
JavaScript Reference Manual:JavaScript while Statement
- Previous Page const
- Next Page debugger
- Go Up One Level JavaScript Statement Reference Manual