DEV Community

Takuya Kikuchi
Takuya Kikuchi

Posted on

break in forEach? 🤔

Conclusion first

We cannot break in forEach loop

What are the alternatives

=> You might get eslint error
no-restricted-syntax - Rules - ESLint - Pluggable JavaScript linter

  • some or every

Here is the case with some()
Array.prototype.some() - JavaScript | MDN

returning true break the loop 👍

Image 2021-08-12 at 4.45.30 PM

Reference

Discussion (1)

Collapse
jonrandy profile image
Jon Randy

Or just throw an exception inside the forEach