totn JavaScript

JavaScript: Continue Statement

This JavaScript tutorial explains how to use the continue statement with syntax and examples.

Description

In JavaScript, the continue statement is used when you want to restart a new iteration of a loop. This statement can be used in a while loop, for loop or for-in loop. If you try using the continue statement in other loop types, you might get unexpected results.

When JavaScript executes the continue statement, any remaining code left in the current iteration of the loop body is skipped. The code will resume execution at the start of the loop (as a new iteration of the loop).

You can also use a continue statement 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 continue statement to restart a loop such as the while loop, for loop or for-in loop.
  • If there are nested loops, the continue statement will restart the innermost loop.

Example

Let's look at an example that shows how to use a continue statement in JavaScript.

How to use the Continue Statement with the While Loop

You can also use the continue statement to restart a new iteration 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 continue statement is used to restart a new iteration of the while loop and skip the remainder 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 before 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 top of the while loop with the following command:

counter++;

We have done this so as to avoid creating an infinite loop in our logic. If our counter had been incremented at the end of the loop, then once the counter is equal to 3, it would get "stuck" at a value of 3 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 iteration of the for loop. Let's rewrite our example 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 continue statement is used to restart a new iteration of the for loop and skip the remainder of the loop body.

This example will output the following to the web 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 web browser console log when the counter is equal to 3. The continue statement has restarted the loop before the following command can be run (but only when the counter is equal to 3):

console.log(counter + ' - Inside for loop on TechOnTheNet.com');