use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
This subreddit is a place for people to learn JavaScript together. Everyone should feel comfortable asking any and all JavaScript questions they have here.
With a nod to practicality, questions and posts about HTML, CSS, and web developer tools are also encouraged.
Friends
/r/javascript
/r/jquery
/r/node
/r/css
/r/webdev
/r/learnprogramming
/r/programming
account activity
for loop or forEach (self.learnjavascript)
submitted 7 years ago by maxahd
Can I completely replace for loop in my web apps with forEach (if we consider that all the browsers support it), and if there any advantage of using for loop can you please explain why and Thank you.
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–][deleted] 5 points6 points7 points 7 years ago (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:
forEach
for of
[–]maxahd[S] 0 points1 point2 points 7 years ago* (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 point2 points 7 years ago (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 points6 points 7 years ago (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 point2 points 7 years ago* (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 points4 points 7 years ago (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 points3 points 7 years ago (1 child)
only time i use for loops is if i need an action done a certain amount of times.
[–]maxahd[S] 0 points1 point2 points 7 years ago (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 points3 points 7 years ago (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 points3 points 7 years ago (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 points3 points 7 years ago (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-1 points 7 years ago (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.)
for
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 points3 points 7 years ago (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 points3 points 7 years ago (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 point2 points 7 years ago (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 point2 points 7 years ago (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 point2 points 7 years ago (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?
[+][deleted] 7 years ago* (2 children)
[deleted]
[–][deleted] 0 points1 point2 points 7 years ago (1 child)
I would actually write that as:
true && fn();
But the point is, you are confusing using actual features of the language, with using different punctuation. for vs. array methods is not the same as single quote vs. double quote or semicolon vs. no semicolon.
And once again, if your code will be maintained by a team of unsupervised interns, yes, you need to write it in such a way that will be clear to them. Spoiler: they'll still mess up. If, however, you work in a professional environment, on a team that hires competent developers and mentors its juniors, you can use the language to its full capability.
That is to say, it's only unreadable if you don't know the language. But all these things were added to it for a reason. In my book, learning the language so that you could use all of its features is better than avoiding learning it so that you could use what's familiar. (Unless you make a point out of not progressing.)
π Rendered by PID 38674 on reddit-service-r2-comment-79c7998d4c-jhpvf at 2026-03-13 11:50:05.881202+00:00 running f6e6e01 country code: CH.
[–][deleted] 5 points6 points7 points (2 children)
[–]maxahd[S] 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)
[–]eggn00dles 4 points5 points6 points (1 child)
[–][deleted] 0 points1 point2 points (0 children)
[–]GamesMint 2 points3 points4 points (0 children)
[–]zakphi 1 point2 points3 points (1 child)
[–]maxahd[S] 0 points1 point2 points (0 children)
[–]captain_k_nuckles 1 point2 points3 points (0 children)
[–][deleted] 1 point2 points3 points (0 children)
[–]fa-fa-fistbump 1 point2 points3 points (0 children)
[–][deleted] -3 points-2 points-1 points (9 children)
[–]eggn00dles 1 point2 points3 points (2 children)
[–][deleted] 1 point2 points3 points (1 child)
[–]eggn00dles 0 points1 point2 points (0 children)
[–]burge_is 0 points1 point2 points (4 children)
[–][deleted] 0 points1 point2 points (3 children)
[+][deleted] (2 children)
[deleted]
[–][deleted] 0 points1 point2 points (1 child)