[javascript] Short circuit Array.forEach like calling break

Yes it is possible to continue and to exit of a forEach loop.

To continue, you can use return, the loop will continue but the current function will end.

To exit of the loop, you can set the third parameter to 0 length, set to empty array. The loop will not continue, the current function do, so you can use "return" to finish, like exit in a normal for loop...

This:

[1,2,3,4,5,6,7,8,9,10].forEach((a,b,c) => {
    console.log(a);
    if(b == 2){return;}
    if(b == 4){c.length = 0;return;}
    console.log("next...",b);
});

will print this:

1
next... 0
2
next... 1
3
4
next... 3
5