DEV Community

Discussion on: JavaScript Arrays: .forEach - The Functional for of loop

Collapse
 
cedpoilly profile image
Cedric

Agreed ;) Thank you for mentioning this. And throwing an exception can stop any kind of code.
Then this technique is a hack, it is not according to the design of the language like the break statement for imperative loops. Nor is it a feature of the forEach loop.
So throwing an exception in a forEach callback is possible, but when you reach that point in your code, refactoring to a for of or a for i loop would actually improve readability. :)

Collapse
 
drozerah profile image
Drozerah

Yes totally agree as mentioned - though, we can wondering why such a break thing is not available for this method ? I mean, particularly, in the way to have a better understanding of the method itself and JavaScript more generaly...