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.
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 thanforEach()
.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 withforEach
.Instead, use
map()
when you plan to change or alternate your data. You can even usefilter()
,sort()
,reduce()
etc. in conjunction withmap()
. You can still do the same thing withforEach()
, but it's preferable to usemap()
because it manipulates your data in an immutable way.Some folks also say that
map()
is faster thanforEach()
regarding performance.However, in the end, It depends on what you’re trying to accomplish.
Awesome, thank you for the detailed response :)