Using Continue in JavaScript forEach() - Mastering JS

PHOTO EMBED

Wed Jul 20 2022 15:53:24 GMT+0000 (Coordinated Universal Time)

Saved by @kalehm

1. Use return
For practical purposes, return in a forEach() callback is equivalent to continue in a conventional for loop. When you return, you skip the rest of the forEach() callback and JavaScript goes on to the next iteration of the loop.

// Prints "2, 4"
[1, 2, 3, 4, 5].forEach(v => {
  if (v % 2 !== 0) {
    return;
  }
  console.log(v);
});
content_copyCOPY

https://masteringjs.io/tutorials/fundamentals/foreach-continue