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)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay