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);
});
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter