We can use the JavaScript continue statement to skip over certain conditions in a for or while loop. The best way to understand the continue statement is with an example.

var count = 0;
for (var i = 0; i < 20; i++) {
  if (i == 5) { continue; }
  //If the above condition is true, then the code below will not be executed
  count = count + i;
}

As you can see in the code above, if the variable i ever is equal to the number 5, then the code count = count + i; will not be executed for that number. But the loop will continue.

If the variable i equals 6, then the code below will be executed for that value.

This can be very useful if you want to iterate through an array and only perform calculations on some items in the array.

Let’s take a look at an example of this below.

Using the JavaScript continue statement to only add the odd numbers in an array

In this example, we will let the user enter in as many numbers as they want, and we will add up only the odd ones.

In the HTML, we will provide code to let the user input as many numbers as they want separated by a comma(,).

Here is the simple HTML set up:

Type as many numbers as you want separated by a comma.

If the numbers are not separated by a comma only, this example will not work.


Get the sum of all odd numbers

In the JavaScript code, we will create an array of numbers from this input.

To create an array from the string of numbers the user provides, we will use the split() method along with the Array map() method.

Once we have our array of numbers, we will use a for loop to loop through each value in the array. We can check whether the number is even or if it is less than 0, and use the continue statement if those conditions are true to skip over adding those numbers.

We will finally update the results using the textContent property.

Here is the JavaScript code:

function addOddValues(){
  
  //Get the user input
  var userInput = document.getElementById("userArr").value;

  //Convert user input to an array of numbers
  var numbersArray = userInput.split(',').map(Number);

  //Add all the odd values
  var sum = 0;
  for (var i = 0; i < numbersArray.length; i++) {
    //Check if number is odd or is a negative number
    if ((numbersArray[i] % 2) == 0 || numbersArray[i] < 0){ 
      continue;
    }
    sum = sum + numbersArray[i];
  }

  //Display the sum of the odd numbers
  document.getElementById("results").textContent = "The sum of the odd numbers is: " + sum;

}

The final code and output for this example is below:

Code Output:

Type as many numbers as you want separated by a comma.

If the numbers are not separated by a comma only, this example will not work.

Get the sum of all odd numbers

Full Code:

Type as many numbers as you want separated by a comma.

If the numbers are not separated by a comma only, this example will not work.


Get the sum of all odd numbers



Hopefully this article has helped you to understand how to use the JavaScript continue statement.

Click here for more information.

Categorized in:

JavaScript,

Last Update: May 3, 2024