DEV Community

Javascript - forEach vs map method in Arrays

Shikhar Saxena on April 01, 2021

Javascript provides us with different functions to make our development life easy but if you have worked with Javascript for a little while, you un...
Collapse
 
raddevus profile image
raddevus

That's a nice article on a topic that needed to be written up (forEach v map).
Very nice that you provided actual reasons for using one or the other. I notice that a lot of people just use map() because it is the newest and shiniest, but forEach() still has its place. πŸ‘πŸ½

Collapse
 
shikharsaxena98 profile image
Shikhar Saxena

Thank you for the support :)

Collapse
 
disgustingdev profile image
disgusting-dev

Just a quick note here:
forEach is mutable - means he operates with the same piece of memory, allocated for defined array, but giving some CPU resources for restructuring this piece after some changes in loop

map is immutable - means it just allocates new place in memory for your results, where RAM is giving storaging resources and CPU does much less

So there is always a choice if you need to calculate slower but to save some memory, or vice-versa

Collapse
 
ui_ao profile image
Ulrich TSAYO

Great explanation!
ThanksπŸ‘ŒπŸ½

Collapse
 
shikharsaxena98 profile image
Shikhar Saxena

Thanks! Let me know if there's some topic you want me to cover

Collapse
 
andrewbaisden profile image
Andrew Baisden

Cool explanation.

Collapse
 
shikharsaxena98 profile image
Shikhar Saxena

Thanks! Let me know if there's some topic you want me to cover

Collapse
 
arjunkumardev profile image
ArjunKumarDev

Great explanation

Collapse
 
mehedihsajib profile image
Mehedi H Sajib

Clear explanation. Thanks <3

Collapse
 
shikharsaxena98 profile image
Shikhar Saxena

Thanks! Let me know if there's some topic you want me to cover

Collapse
 
ankitasehgal98 profile image
Ankita Sehgal

Great work