JavaScript ForEach Continue Example

JavaScript ForEach Continue Example | The continue keyword will result in an error if used inside of a JavaScript forEach loop. Instead, since you send a callback into the forEach statement and it behaves in the same way, you will need to use the return statement in place of the continue statement.

Because a callback function is required when using a forEach loop in JavaScript, employing a return statement to resume the loop works.

When your callback function completes and has finished running the code inside of itself, it is the only time you can move on to the following iteration. Therefore, a return statement is what you need in place of a continue statement if you want to end and finish the function as soon as possible.

JavaScript ForEach Continue Example

Here is an illustration of a JavaScript forEach continue statement that uses a return statement:-

[1, 2, 3, 4].forEach((item) => {
  if (item > 2) {
    return; // emulating JavaScript forEach continue statement
  }
  console.log(item);
});

Output:-

1
2

In other words, if you want to keep going in a JavaScript Array.prototype.forEach loop, you can use the return to exit the function, which will have the same result because it will then move on to the next iteration.

Why continue is incompatible with JavaScript’s forEach loop? Because you can only use a continue statement in a block immediately for a loop – which may be a for loop or a while loop – you can’t use one in a forEach loop.

If you constructed a function inside the loop, for example, you still can’t use the continue statement inside that function. This is true even if you are in a for or while loop.

Here is an illustration of this mistake:-

[1, 2, 3, 4].orEach(a => {
    continue; // error
    console.log(a)
})

Output:-

Uncaught SyntaxError: Illegal continue statement: no surrounding iteration statement

In the JavaScript forEach loop, there are a few alternatives to the continue statement. The return statement is the ideal option, as we have already discussed, but if for some reason this is not an option for you, there are a few other options.

The options to use a JavaScript forEach continue statements are listed below:-

  • The return statement is the ideal choice since, unlike forEach, it produces results that are quite comparable to those of a continue statement.
  • Another excellent choice is to use a for loop or a while loop, which enables you to use the continue statement as you would anticipate.
  • The last and least efficient approach is to sort or filter the array using Array.prototype.filter. The objective is to remove all entries from the array that you would have continued with so that you don’t have to skip over them.
  • Just to be clear, the least ideal technique to simulate a continue statement is by eliminating the values you want to skip over from an array using Array.prototype.filter.

It implies that you will ultimately need to run an additional loop, which is not ideal in and of itself, but it also implies that you will not be able to carry out the same calculations in the same order as you would have by using the continue statement.

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published. Required fields are marked *