I recommend to always use for … of in ES6.
- It works on any iterable.
- It supports all kinds of control flow in the loop body, like
continue,break,return,yieldandawait.
Also I personally find it more readable, but that comes down to preference. Some people think forEach is a more functional style, but that's wrong – it has no result value and is all about doing side effects, so an imperative-looking loop fits that purpose better.
Performance is not a concern: in modern engines all loop styles are equivalent.
Answer from Bergi on Stack OverflowI recommend to always use for … of in ES6.
- It works on any iterable.
- It supports all kinds of control flow in the loop body, like
continue,break,return,yieldandawait.
Also I personally find it more readable, but that comes down to preference. Some people think forEach is a more functional style, but that's wrong – it has no result value and is all about doing side effects, so an imperative-looking loop fits that purpose better.
Performance is not a concern: in modern engines all loop styles are equivalent.
This is a very interesting question which has been discussed in many other sites. I'll post the basics of what I have read.
ForEach exclusively belong to the royal family of Arrays. The forEach method was introduced with lineage to the prototypal inheritance of Array object! Needless to say, the forEach clause works only with those data structure which are Arrays. The method basically iterates over the elements of the array and executes a callback function [basically some executable function/ fun activity].
The for-of loop is adequately new to the JS world and packs in super-powers! Voilaaaaaaa! The for-of loop creates a loop iterating over iterable member objects. The list is an extensive one such as
- Array
- Map
- Set
- String
- TypedArray
- Other W3C classes
You need to know that this bad-ass boy emerged with the birth of ES6 in 2015. So, it offers plenty of flexibility in usage
Performance
In performance, for...of is faster than forEach. Results can be found here
forEach is 24% slower than for...of
Update
There are several other iterable classes in the W3C specification, like FileList, as I mentioned above. And in recent drafts of W3C (around when ES6 was released), collections like HTMLCollection and NodeList now implement forEach() as well, not just Array anymore. By @Patrick Roberts
Source Links:
- https://codeburst.io/foreach-vs-for-of-vs-for-in-tug-of-for-d8f935396648
- https://www.reddit.com/r/javascript/comments/4spd5b/forof_vs_foreach/
Videos
Whenever i loop through an array i personally prefer to use array.foreach to apply something to whatever element i wish to edit, but the industry standard seems to be using for loops. Is this becasue the industry isnt fully used to ES6 notation or is either method good in its respective ways? Anyways which method do you prefer to use, and why?
If you want to do sequential async tasks, you can't use forEach, you need to use a for loop with await.
Foreach, Map, Reduce, Filter, for..of, all these are good.
Now there are some cases where for..of or for loop will be substantially faster than a foreach for example, mainly because of the overhead caused by the callback in the foreach. But these are extreme cases, pretty rare, involving a high amount of elements being looped through.
In these cases you should always do some benchmark with different options to analyse the results and pick the best one anyway.
I prefer to use the aggregate functions over for loops because they're more expressive in my eyes. It's purely subjective though, and all the other options are fine too.
I'm now almost exclusively using for...of statements instead of .forEach() and I'm wondering - is this just preference or am I doing it "right"/"wrong"? To my mind for...of breaks the loop cleanly and plays nice with async but are there circumstances where .forEach() is better?
I'm wondering if there are any significant differences between doing:
for (const item of items) { ... }and
items.forEach(item => { ... });
for..of works with any iterable, whereas forEach is only for arrays. forEach gives you access to the index, if you want it, whereas for..of does not.
And no, forEach is NOT asynchronous. Both examples are synchronous.
Also my two cents:
-
for..of is more performant than .forEach
-
you can use async/await inside for..of but not inside .forEach
let totalScoreGain = 0; for( let user in scoredUsers ) { totalScoreGain += await user.addScore( 50 ); } console.log( totalScoreGain );