JavaScript Continue Statement

The JavaScript Continue Statement is another one that controls the flow of loops. The JavaScript Continue statement used inside For Loop, While Loop and Do While Loops. While executing these loops, if the compiler finds the continue statement inside them, the loop will stop the current iteration and starts the new iteration from the beginning.

For example, there are 10 statements inside a loop. And we want to skip executing the last five statements (statement 6 to 10) when a certain condition is True or else it has to run all the ten statements. In these situations, we place the condition after the 5th statement, followed by JavaScript continue statement. If the condition is True, it will stop executing statements 6 to 10. Otherwise, it will execute statements 1 to 10.

JavaScript Continue Statement Examples

In this article, We would like to share two examples to display the working functionality of the JavaScript Continue statement in both For loop and While loop. The syntax of the Continue Statement in JavaScript is as follows:

continue;

JavaScript Continue Statement inside For Loop

Use JavaScript Continue Statement inside the For Loop with example. This program allows the user to enter any integer values. Then it will display the Even and Odd numbers inside the integer value.

<!DOCTYPE html>
<html>
<head>
    <title> JavaScript Continue Statement in For Loop </title>
</head>

<body>
    <h1> JavaScript Continue Statement </h1>
<script>
  var i;
  for(i = 1; i <= 10; i++)
  {
    if (i % 2 !== 0)
    {
      document.write("<br \ ><b>Odd Number</b>= " + i +"(Skipped by Continue)");
      continue;
    }
    document.write("<br \ ><b>Even Numbers </b>= " + i);
  }
</script>
</body>
</html>
JavaScript Continue Statement 1
  • Here we are not going to explain the For Loop.
  • Inside the for loop, we placed If Statement to test whether (i%2 != 0). If this condition is True, the continue statement will execute, and the iteration will stop at that number without printing the other statements.
  • If the condition is false, it will skip the continue statement and prints that number as output (In Our case Even Number)

JavaScript Continue in While Loop Example

Let me use the JS Continue Statement inside a While Loop with example.

<!DOCTYPE html>
<html>
<head>
    <title> JavaScript Continue Statement in While Loop </title>
</head>

<body>
    <h1> JavaScript Continue Statement </h1>
<script>
    var i = 0;
    while (i <= 10)
    {
        if (i == 5 || i == 9)
        {
            document.write("<br \ ><b>The Skipped Values </b>= " + i);
            i++;
            continue;
        }
   
        document.write("<br \ ><b>The Value of the Variable </b>= " + i);
        i++;
    }
</script>
</body>
</html>
JavaScript Continue Statement

The Value of the Variable = 0
The Value of the Variable = 1
The Value of the Variable = 2
The Value of the Variable = 3
The Value of the Variable = 4
The Skipped Values = 5
The Value of the Variable = 6
The Value of the Variable = 7
The Value of the Variable = 8
The Skipped Values = 9
The Value of the Variable = 10

In this JavaScript Continue Statement example, we are not going to explain the While Loop.

Inside the While loop, we placed JavaScript If Statement to test whether i is equal to 5 or 9. If this condition is True, the JS continue statement will execute, and the iteration will stop at that number without printing the other printf statement: printf(“Values are: %d “, i);.

For better understanding, we placed the below statement inside the JavaScript If condition. So, whenever the iteration break, that value will print from this statement.

document.write("<br \ ><b>The Skipped Values </b>= " + i);

If the condition is false, then it will skip the continue statement and prints that number as output (In Our case 0,1,2,3,4,6,7,8,10)