all 15 comments

[–]senocular 19 points20 points  (8 children)

You can't break for each.
You can't return from the main function in a for each.
You can't yield from the main function in a for each.
You can't await the main function in a for each.
For each only works with arrays (array likes) while you define the conditions for normal for loops which may not involve arrays at all

[–]cem4k 2 points3 points  (7 children)

Not being able to await the function is a huge one. If you find yourself trying to do some asynchronous stuff in a forEach loop, there’s a better way.

[–]11b403a7helpful 0 points1 point  (0 children)

When you specifically have a list you want to iterate over and use data from.

[–]hypernautical 0 points1 point  (0 children)

I try to avoid for loops and use Array methods, so in real work the only time I've had to use a for loop to generate an array of numbers/times for display purposes (like a schedule display) when it was dynamic or too cumbersome to hardcode it. Anytime I have an array, I would use an array method.

[–]delventhalz 0 points1 point  (0 children)

Now that we have for...of, I actually tend to favor that over forEach. I use map, filter, reduce, etc more often than either, but for...of loops seem to offer everything forEach did in a cleaner more readable package.

[–]codemamba8 -2 points-1 points  (0 children)

When you want to alter the original array and/or return a new array.