DEV Community

Cover image for map() vs forEach() in JS
kiran ravi
kiran ravi

Posted on

map() vs forEach() in JS

map()
The map() method loops through an array
and returns a new array with transformed
elements. It does not modify the original
array.

JS map()

forEach()
The forEach() method loops through an

array and executes a function on each

element but does not return a new array

JS forEach()

Conclusion

  • map() returns a new array with modified values.
  • forEach() does not return an array, only executes a function.
  • Use map() when you need transformed data.
  • Use forEach() for side effects like logging or API calls.

Top comments (0)