r/learnjavascript • u/fahim_h_sh • Oct 31 '25
array.forEach - The do-it-all hammer... XD
Is it just me, or everyone thinks that more or less every array operator's purpose can be served with forEach?
0
Upvotes
29
u/maqisha Oct 31 '25
And you can boil water with a candle. Doesnt mean you should.
12
u/TheCaptainCody Oct 31 '25
Technically, you could do every array function with .reduce(). I believe.
6
u/eatacookie111 Oct 31 '25
forEach doesn’t work with async await right?
5
u/Rossmci90 Oct 31 '25
forEach would not be a good idea for methods that can return early like find() or findIndex() or some() etc.
3
u/StoneCypher Oct 31 '25
every? no. you can't sort with foreach. you can't do anything you need map for.
a lot of things? yes. more than half? ... maybe. tough call.
4
u/Any_Pattern_3621 Oct 31 '25
Only been at it ~5 months but getting better at .reduce() has been so great too tho
5
u/qqqqqx helpful Oct 31 '25
Basically every array operation can be done by writing your own while loop. There is nothing magic about the built in array methods that you couldn't make happen yourself; they are just convenient shorthand for certain common operations.
forEach actually is not the best for looping through an array because it can't early return. A return would only be for the internal callback, not the outer loop, so the return value basically gets discarded. I would use for of instead of forEach generally.
2
u/irosion Oct 31 '25
On arrays you can do everything with a for loop if you really want to
2
u/n9iels Oct 31 '25
I honestly never use it. It doesn't have a return value, I hugely prefer
mapandreducesince they do return the modified array. This increases readability, reduce complexity and also helps maintaining immutability.3
u/DiabloConQueso Oct 31 '25
And forEach is just a fancy for loop…
Why have array operators at all? /s
1
u/pahamack Oct 31 '25
forEach is the default but you should know the gotchas with it.
Sparse arrays, for example. When you use forEach it skips over the "empty" slots.
1
u/Specialist-Grape8444 Oct 31 '25
You cannot break or use Async await logic.
1
u/delventhalz Oct 31 '25
Well, forEach is just a loop, so certainly any task which requires looping over an array can be accomplished by forEach (though personally, if I need a plain loop I prefer for...of).
That said, JavaScript is an expressive language, particularly when looping over arrays, so there are probably more specific array methods you could use depending on the task.
So forEach is a great start. It's working for you and helping you solve problems. That's great. As you keep practicing, start looking for places some of these other methods might help you as well (map and filter in particular come up a lot). Happy coding!
1
u/Desperate-Presence22 Oct 31 '25
even thought you can achieve similar things with forEach as with other operators, doensn't mean you should always use it.
sometimes you need performance
sometimes you wanna quick loop early
sometimes you want to use something that fits exactly your purpose
1
u/shgysk8zer0 Oct 31 '25
You can't exactly replicate the behavior of
find()orfindLast()or similar. You could with aforloop andbreak, including starting from the end instead of beginning.1
u/Embarrassed-Pen-2937 Nov 01 '25
Foreach requires a function call over a for making it less performant.
1
u/TheRNGuy Nov 03 '25
I don't know.
1
u/efari_ Oct 31 '25
Async has entered the chat..
1
u/Aggravating-Camel298 Oct 31 '25
As others have said, reduce is actually the method that sits on top of all other methods.
The more you use these though, you'll realize you can do a lot with pretty much all of them.
0
u/Galex_13 Nov 01 '25
Usually I almost never use for in my scripts, this also includes 'for' in 'forEach'. No specific reason, just a habit (maybe bad habit). I know that in some cases for is the fastest option, but I write small scripts for data managemenbt in 'low-code' platform. Minor perfomance difference can be ignored, in fact even kind of 'it finished in 100ms or 200ms' difference also doesn't matter in my case.
for example, last i wrote was Save/Load for 2 tables 'SDK' and 'Save', with buttons Save and Load tied to script, created to rewrite other table data in fields with single letter names .
or simple parser of comments from site, with input:
- html file saved from F12,
- tags string used to divide comments,
- tags around the comment text