filmov
tv
JavaScript | How can you break out of a forEach loop?
Показать описание
If you think about what actually happens when you call [].forEach and maybe see it in code, it's easier to understand why it's impossible "break" out of a forEach loop in JavaScript.
You could throw an exception to force your way out or hack your way around it by implementing your own version of forEach like in my example here. But honestly, just use for..of or a classic for loop, in cases where you need to break out of a loop.
You could throw an exception to force your way out or hack your way around it by implementing your own version of forEach like in my example here. But honestly, just use for..of or a classic for loop, in cases where you need to break out of a loop.