DEV Community

Discussion on: JavaScript loop querySelectorAll results

Collapse
 
grayedfox profile image
GrayedFox

Not according to this code I just threw into my browser console:

[0, 1, 2, 3, 4].forEach(e => { if (e == 3) { return; } console.log(`${e}`)})
Enter fullscreen mode Exit fullscreen mode

The above logs 0, 1, 2, undefined, 4. Also the MDN docs state in a big yellow warning box "There is no way to stop a forEach() loop other than by throwing an exception": developer.mozilla.org/en-US/docs/W...

They also recommend using for...of or for...in if breaking out early is desired :) 🙇

Thread Thread
 
dailydevtips1 profile image
Chris Bongers

Ah good to know indeed!
In most cases, I always want to attach listeners to all, but indeed strong argument!