This JavaScript tutorial explains how to use the continue declaration with syntax and examples.
Description
In JavaScript, the continue assertion is used when you favor to restart a new iteration of a loop. This assertion can be used in a whilst loop, for loop or for-in loop. If you strive using the proceed announcement in different loop types, you may get surprising results.
When JavaScript executes the proceed statement, any final code left in the present day generation of the loop physique is skipped. The code will resume execution at the start of the loop (as a new generation of the loop).
You can also use a proceed declaration to execute a labeled statement.
Syntax
The syntax for the continue statement in JavaScript is:
continue [label_name];
Parameters or Arguments
label_name Optional. An identifier name (or label name) for a statement.
Note
You use the proceed assertion to restart a loop such as the whilst loop, for loop or for-in loop. If there are nested loops, the proceed assertion will restart the innermost loop.
Example
Let’s appear at an instance that indicates how to use a proceed announcement in JavaScript.
How to use the Continue Statement with the While Loop
You can additionally use the proceed declaration to restart a new new release of the while loop.
For example:
var counter = 0;
while (counter < 5) {
counter++;
if (counter == 3) {
continue;
}
console.log(counter + ' - Inside while loop on TechOnTheNet.com');
}
console.log(counter + ' - Done while loop on TechOnTheNet.com');
In this example, the proceed declaration is used to restart a new iteration of the whilst loop and ignore the the rest of the loop body.
This example will output the following to the web browser console log:
1 - Inside while loop on TechOnTheNet.com
2 - Inside while loop on TechOnTheNet.com
4 - Inside while loop on TechOnTheNet.com
5 - Inside while loop on TechOnTheNet.com
5 - Done while loop on TechOnTheNet.com
As you can see, an entry is not written to the web browser console log when the counter is equal to 3 The continue statement has restarted the loop earlier than the following command can be run (but only when the counter is equal to 3):
console.log(counter + ' - Inside while loop on TechOnTheNet.com');
TIP: Notice that in the above example, we have incremented the counter variable at the pinnacle of the whilst loop with the following command: counter++; We have executed this so as to keep away from developing an endless loop in our logic. If our counter had been incremented at the quit of the loop, then once the counter is equal to 3, it would get “stuck” at a cost of three and the while loop would never terminate.
How to use the Continue Statement with the For Loop
You can also use the continue statement to restart a new generation of the for loop. Let’s rewrite our instance with the for loop.
For example:
for (var counter = 1; counter < 5; counter++) {
if (counter == 3) {
continue;
}
console.log(counter + ' - Inside for loop on TechOnTheNet.com');
}
console.log(counter + ' - Done for loop on TechOnTheNet.com');
In this example, the proceed statement is used to restart a new iteration of the for loop and pass the remainder of the loop body.
This instance will output the following to the net browser console log:
1 - Inside for loop on TechOnTheNet.com
2 - Inside for loop on TechOnTheNet.com
4 - Inside for loop on TechOnTheNet.com
5 - Done for loop on TechOnTheNet.com
In this example, an entry is not written to the internet browser console log when the counter is equal to three The continue announcement has restarted the loop before the following command can be run (but solely when the counter is equal to 3):
console.log(counter + ' - Inside for loop on TechOnTheNet.com');
Leave a Review