In programming there is often a need to repeat one action several times. For example, when you want to print a list of names or just go through the numbers from 1 to 10 and execute the same code for each of them. Those are situations where loops are indispensable.

There are several types of loops in JavaScript such as "while", "for" or "do…while". All of them are very convenient for various purposes. However, sometimes you might need to stop the execution of your cycle (that's when you use the break directive) or just jump to the next iteration. For the latter case JavaScript offers a special statement - continue. This directive is used when there are no actions to execute at the current iteration anymore.

Examples of continue

For instance, let's create a program to print odd numbers. You might want to use the for loop and for each number check if the remainder of its division by 2 is 0. If it is so, then you need to use continue which will switch your loop to the next iteration, otherwise the program will print the correct number in the console.

for (i = 0; i < 10; i++)
{
if (i % 2 == 0) continue;
console.log(i);
}
// logs "1, 3, 5, 7, 9"

In fact, you could omit the continue statement and just create another block of expressions after the if condition. The code below is identical to the one above. However, when creating complicated programs with many different commands the readability of your code will be much worse with the additional curly brackets. That's why the use of continue is encouraged.

for (i = 0; i < 10; i++)
{
if (i % 2) {
console.log(i);
    }
}
// logs "1, 3, 5, 7, 9"

A short way of writing an if-else clause is a ternary operator (? :). For instance, if you try to rewrite the above code in a shortened way using a ternary operator, you will probably get something like in the below example.

for (i = 0; i < 10; i++)
{
 (i % 2) ? console.log(i) : continue;
}

Unfortunately, this code will lead you to a syntax error saying that "continue" is an unexpected token. The thing is that this directive is not an actual expression so you are not allowed to use it with a ternary operator. You can consider it as one more reason not to use a ternary operator instead of if-else clause.

Using a continue statement with a while loop is also possible. It will move your program to checking the condition stated in the beginning again. In the following example you can see how the continue directive allows skipping the current iteration if the number is not 2 and not 6. Logical AND operator is also used for this purpose.

i = 0;
while (i < 10) {
    i++;
    if (i!=2 && i != 6)  continue; 
    console.log(i);       
} // logs "2, 6"

Basically, this program checks if i is less than 10. If true, the variable is incremented and then another condition is checked. If i is not equal to 2 and not equal to 6, the program ends the current iteration and goes to check the initial statement again.

If it's still less than 10, the operation is repeated. If i turns out to be equal to 2 or 6, it will be printed in the console. Thus, you will only get "2, 6" represented.

Note that your program will not execute the code written after continue and will start working with commands in the beginning of the loop if the condition is true. That's why in the program above the incrementing statement is written at the top of the loop. You must keep it in mind in order to avoid creating an infinite cycle.

Labels for continue statement

There are situations when you have several loops working at the same time. Simple use of continue may not lead to the expected result as it will just jump to the next iteration in the current loop instead of the outer ones. However, you can achieve the desired outcome by using labels.

The label is a sort of an identifier followed by a colon and written in front of your loop. It's as if you give your cycle a special name. Then while working with nested loops you can easily jump over iterations in any of them, using the label created earlier. The syntax is easy enough. It's shown in the example below.

outer: for (i = 0; i < 5; i++) {
  inner: for (j = 0; j < 10; j++) {
    if (j == 3) continue outer;
    console.log('i = ' + i + ', j = ' + j);
  }
}

This way, you can see that two labels for two different loops were created. Using one of them helps you to go to the next iteration in the outer cycle instead of the inner one.

Pay attention that labels will not allow you to jump in any place in your code. Execution of continue is possible strictly inside a loop and the label must be specified somewhere above this command. For example, this code will not work:

outer: for (i = 0; i < 5; i++)
{
    if (i == 3) continue inner;
        inner:  for (j = 0; j < 10; j++) 
            {

In conclusion, for convenient working with loops there is a special statement in JavaScript - continue. It allows you to skip the current iteration of the loop and move to the next one. Using labels will help you to work with multiple nested loops efficiently.

The continue directive can simplify your code and prevent you from using curly brackets. However, you can work without it as well. That's why in practice this command is used quite rarely.