JavaScript map() vs forEach(): Which One Should You Use?
Understand the difference between JavaScript map and forEach, when each returns a value, and how to transform arrays without accidental mutation.

map() vs. forEach(): Which One Should You Use?
You have an array and want to do something with every item. Should you use map() or forEach()? Use map() when you need a new array of transformed values. Use forEach() when you want to perform an action for each item and don't need a returned array.
You can run these examples in a modern browser console or recent Node.js. We'll start with the same data and see how the results differ.
map() creates a new array
map() calls its callback for each item and puts each returned value into a new array:
const prices = [10, 20, 30];
const pricesWithTax = prices.map((price) => price * 1.1);
console.log(pricesWithTax); // [11, 22, 33]
console.log(prices); // [10, 20, 30]
The original array is unchanged here, and pricesWithTax contains the transformed values. map() is useful when you want to turn one array into another—for example, preparing data to render in a UI.
In React, a common pattern is returning an element for each item:
const names = ["Mina", "Rafi"];
function NameList() {
return (
<ul>
{names.map((name) => (
<li key={name}>{name}</li>
))}
</ul>
);
}
Each callback returns an <li>, so map() returns an array of elements for React to render.
forEach() performs an action
forEach() also calls a callback for each item, but it ignores callback return values and returns undefined itself:
const prices = [10, 20, 30];
const result = prices.forEach((price) => {
console.log(`Price: ${price}`);
});
console.log(result); // undefined
Use forEach() for a side effect, such as logging or updating an external system. If you need to collect returned values into a new array, use map() instead.
Compare them side by side
| Method | What the callback does | Method returns | Typical use |
|---|---|---|---|
map() | Returns a value for each item | A new array | Transforming data |
forEach() | Performs an action; its return is ignored | undefined | Logging or other side effects |
Both methods visit items in order. Neither method changes the array by itself, but either callback can still mutate an object or other external state. Be intentional about side effects.
A common map() mistake: forgetting to return
If you use braces in an arrow callback, you need an explicit return. Otherwise map() puts undefined in the output for each item:
Incorrect:
const numbers = [1, 2, 3];
const doubled = numbers.map((number) => {
number * 2;
});
console.log(doubled); // [undefined, undefined, undefined]
Correct:
const doubled = numbers.map((number) => {
return number * 2;
});
console.log(doubled); // [2, 4, 6]
Or use the concise expression body, which returns implicitly:
const doubled = numbers.map((number) => number * 2);
map() doesn't guarantee deep immutability
map() creates a new array, but it is a shallow transformation. If the array contains objects, the new array initially contains references to those same objects. Mutating an object inside the callback can therefore change data seen through the original array too.
To create updated objects, return new objects instead:
const users = [
{ name: "Mina", active: false },
{ name: "Rafi", active: false },
];
const activeUsers = users.map((user) => ({
...user,
active: true,
}));
The array is new, and the updated user objects are new as well. This is useful in UI state updates where you want to avoid changing existing objects.
Common beginner mistakes
Using forEach() when a new array is needed
forEach() returns undefined. Assigning its result to a variable won't collect values. Use map() when each item should produce a value in a new array.
Using map() only for side effects
If you ignore the array map() returns, you probably want forEach() instead. Choose the method that describes what your code is doing.
Assuming map() can never mutate data
map() itself doesn't mutate the original array, but its callback can mutate objects or external state. Return new objects when you need an immutable update.
Practice
Given an array of numbers, use map() to create a new array with each value increased by five. Then use forEach() to log each result with its index. Check that the original array still contains the original numbers.
What we learned
map()returns a new array containing each callback's result.forEach()calls a callback for each item and returnsundefined.- Use
map()to transform data; useforEach()for actions that don't need a returned array. map()doesn't mutate the original array by itself, but callbacks can still mutate objects or other state.- In React,
map()is often used to turn data into a list of elements.
What's next?
Next, we'll compare filter() and find(): one keeps every item that matches a condition, while the other returns the first match.
Conclusion
Ask what you need from the operation. If every item should produce a value in a new array, use map(). If you're simply doing something for each item and don't need a result array, use forEach(). That small choice makes your code's intent much easier to read.
Written by
Dev Likhon
Full Stack Developer & Tech Educator
6+ years of professional development experience.
About Me
Comments
Share what you learned or ask a question.
Loading comments...