DEV Community

Piyush Panchal
Piyush Panchal

Posted on

What's the difference between map() and forEach() in JS?

Arrays are super useful and widely used data structures. It also has a ton of different functions which can make our work so much easier. The functions that many people get confused about is map() and forEach() and it's reasonable both do almost the same thing. I personally made this mistake so many times when I was a beginner. The similarity is- that they both run a function or a piece of code for each of the element of the array. But forEach() does not return any new array. It just runs the code for the elements. map() on the other hand returns a new array with the result. Hope this helped.

Thanks for reading.

Discussion (2)

jpomykala profile image
Jakub Pomykała

Hey great reading! Checkout also mine post about i18n libraries for ⚛️

programmerpiyush008 profile image
Piyush Panchal Author

Thanks! Will sure check it out.