JavaScript: Using a labeled continue statement

In Javascript, we all know that there is a Continue statement where its sole purpose is to jump and iteration from being executed. Continue statement is used up in a loop to skip or jump an iteration from ever being executed with a specified or provided condition for it to jump.

It continues the flow of the iteration after the jump and proceeds to perform the next step of iteration without any obstruction. Now at times if required we can use the labeled continue statement too for the right loop it will be a perfect asset.

Syntax

continue LabelName;

Let us see it in action we will be using two for-loop within one another and be iterating the y and z to get the series of numbers as counts.

Example

var y,z;
JumpLabel:for (y = 0; y < 6; y++)
{
    for(z=0;z<y;z++)
    {
        if(y==4)
            continue JumpLabel;
        console.log(y+"\t");
    }
   console.log()
}
console.log("Outside loop");

Output:

1

2
2

3
3
3

5
5
5
5
5

Outside loop
  • So from we have taken two loops and placed a loop within a loop to iterate the Y value within Z and get the series of numbers in the form of count representation.
  • So here we will be using the label called JumpLabel to call the loop to perform the continue on it.
  • Provided with the if condition within the loop that when the iteration comes to the point of 4 then the iteration jumps leaving 4 behind and continuing further with the iteration.
  • This is how we can use the labeled continue statement.
Fri, 12/24/2021 - 08:04

Authored by

Ashwin is working with iVagus as a Technical Lead. He has experience in various technologies like Cypress, JavaScript, TypeScript, Rest Assured, Selenium, Cucumber, and Java.
Tags