Note: I want to break the while()
loop using the forEach
callback function, not to break the forEach
itself.
I'm currently trying to break a while
loop in a forEach method inside that same while loop, but Javascript does not like break
statements inside functions. It spits out unsynctatic break
or illegal break statement
depending on the environment.
I'm trying to do something like this:
while(true) {
example = [false, false, true];
example.forEach(function breakWhile(element) {
if(element) {break;}
})
}
I'm aware that for...of
(plus using a label on the while
loop) solves this, but is it really my only way out? I quite prefer using array.forEach
. And even worse, I could want to use a function instead, which is also underappreciated by JS engines.