For further actions, you may consider blocking this person and/or reporting abuse
Read next
FATAL ERROR: Ineffective mark-compacts near heap limit Allocation failed - JavaScript heap out of memory on Digital OceanDroplet
Sammy Barasa -
Incremental Hydration in Angular 19: Take Your App’s Performance to the Next Level
Naveed Ahmed -
How to create an App Service Application and upload into it (WEB APP)
Adedapo -
Introducing Pretty-js-log: Add Beautiful, Colorful Logging to Your Node.js Apps 🎨
Noureddine Belguinan -
Top comments (6)
Very easy, if you need to return an array, use map. Otherwise forEach.
Both iterate over the collection, one returns values the other doesn't. There's no difference in the iteration part.
People like to make rules on when to use them but I use map, and reduce way more than any other method regardless of mutability. Mutability is a choice of what we do with the data, not the function's themselves. We can choose to mutate or not using just map.
Map for immutable new array, forEach for mutations / side effects.
Like others have said, they both do very different things. If you don't need to return an array, use
forEach
:)Thank you so much for your opinions !
Depends on the usecase. I like map.