loading...

re: 15 must-know JavaScript array methods in 2020 VIEW POST

TOP OF THREAD FULL DISCUSSION
re: Awesome post, thank you :). Just a curious question (junior dev), should we avoid using forEach() because it'll mutate the data?
 

forEach() is not bad, it's not like DON'T USE IT. But it depends on the use case.
If you tend to functional programming, immutability and all that kind of stuff, you should use map() more often than forEach().

forEach is preferable when you're planning to do something like logging out your data or save the data to a database. But don't change your data with forEach.
Instead, use map() when you plan to change or alternate your data. You can even use filter(), sort(), reduce() etc. in conjunction with map(). You can still do the same thing with forEach(), but it's preferable to use map() because it manipulates your data in an immutable way.
Some folks also say that map() is faster than forEach() regarding performance.
However, in the end, It depends on what you’re trying to accomplish.

 

Awesome, thank you for the detailed response :)

code of conduct - report abuse