Advertisement
Beginner JavaScript, Arrays & Array Methods

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

Short answer

map() returns a new array built from the return value of the callback for each element; forEach() only runs the callback for its side effects and always returns undefined.

Save JavaScript mock interview
Advertisement
AModel answer
map() returns a new array built from the return value of the callback for each element; forEach() only runs the callback for its side effects and always returns undefined.
const doubled = [1,2,3].map(n => n*2); // [2,4,6]
Real-world example
Transforming an array of user objects into an array of display names.
Likely follow-ups
  • Can you chain map() and filter() together?
Related topics