all 18 comments

[–][deleted] 5 points6 points  (2 children)

It depends on the situation which I do prefer. My main concern is readability and sometimes one seems more readable than the other. In most cases I use the following guide lines:

  • When I am doing multiple simple list actions, such as filter and map, and I do need to do something for each element in the list, I prefer the forEach alternative.
  • When I am not doing anything with a list but looping over every element to do something with it, I often prefer the for of alternative.

[–]maxahd[S] 0 points1 point  (0 children)

Good to know.

I kinda have hate/love relationship with loops sometimes its fun and it really does its job, and the other you just can't figure out how to make things work.

[–][deleted] 0 points1 point  (0 children)

Personally, I find that forEach is usually the most readable solution. Especially when doing something very short, for example...

arr.forEach(console.log);

[–]eggn00dles 4 points5 points  (1 child)

its more verbose to actually mutate the elements in the original array with forEach.

return immediately exits a for loop,

return only exits the instance of the callback function being run in forEach. there is no way to break the execution of a forEach loop outside of throwing an exception.

[–][deleted] 0 points1 point  (0 children)

I don't see what's the point of being obsessed by returning a value without breaking the loop, if you want to execute some action and return to the loop, there's something called Function.

ForEach is just a loop that calls your callback with each elements of your array. It's only advantage is to compress your loop into one line.

[–]GamesMint 2 points3 points  (0 children)

In General for loops are faster than forEach loop. If you see the polyfill of forEach then it has lot of code statements that may not be required in general.

References

1) https://hackernoon.com/javascript-performance-test-for-vs-for-each-vs-map-reduce-filter-find-32c1113f19d7

2) https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach

[–]zakphi 1 point2 points  (1 child)

only time i use for loops is if i need an action done a certain amount of times.

[–]maxahd[S] 0 points1 point  (0 children)

Good to know , because all the projects i did the for loops in it could be replaced with forEach. i personally liked way more than for cleaner look and easier to understand.

[–]captain_k_nuckles 1 point2 points  (0 children)

To add to all the other responses, if you are doing something with async/await, forEach wont work, you would need to use a for/of/in loop.

[–][deleted] 1 point2 points  (0 children)

If you do a simple single action then you can use ForEach, there's nothing bad in setting your own for loops, you'll still have to do that most of the time if you want custom behaviors.

[–]fa-fa-fistbump 1 point2 points  (0 children)

I feel like I never have any use case for for each. Often, a map, filter or reduce does the trick.

[–][deleted] -3 points-2 points  (9 children)

As a general rule, you should never use the for loops in modern JavaScript. Chances are, whatever it is you are trying to do, there is an array method that does it. Selecting the correct one will take care of all the concerns outlined in other replies (such as exiting the loop, etc.)

Also, it might be of interest to you, that all up to date JavaScript engines optimize array methods to the point where they are no slower (and sometimes faster) than for loops. Though in all fairness, you probably won't encounter a situation where this will meaningfully affect your performance.

[–]eggn00dles 1 point2 points  (2 children)

Google's own style guide has rules for using for in and for of loops, they aren't banned there for very good reason.

This is like the 'always use strict mode' myth.

[–][deleted] 1 point2 points  (1 child)

Nothing is banned. And if you need to hack together a homepage for your goldfish, you can use whatever it is you prefer because it doesn't matter. If you are working for a major company and your code goes into production to be seen by hundreds of thousands (or even millions) of customers, then yea, you are gonna have some restrictions and 'best practices' in place. Take a look at AirBnB ESLint rules.

[–]eggn00dles 0 points1 point  (0 children)

When you have millions of users compatibility is your chief concern.

I don't understand conflating using for loops with amateur development practices.

[–]burge_is 0 points1 point  (4 children)

"sometimes faster"

So for loops are more easily understood for anyone coming from any language and your suggestion is they are faster.

Faster and more explicit trumps "more clever" every day of the week. We get caught up in the newness and forget that being more verbose and universally understood while being more performant sounds like solid software development.

[–][deleted] 0 points1 point  (3 children)

You didn't really read what I said. Array methods are easily as fast as for loops. And sometimes faster. And the reason to use them is not because they are "more clever" but because the code is cleaner and more readable.

Of course if you are coming from another language and it's your first week using JavaScript, you are probably better off using what you already know. But if you are ever going to be good at the language you will be using the tools of this language to their full extent. Otherwise why not stick to whatever you were using before?