Back to results for “Map”
JavaScript › Arrays & Array Methods

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

Beginner

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.

Common follow-ups: Can you chain map() and filter() together?