JavaScript Notes
Understand the exact differences between JavaScript map() and forEach(): return values, chaining, performance, when to use each, comparison table, and interview Q&A.
Both map() and forEach() iterate over every element in an array. But they serve completely different purposes. Understanding when to use each is a hallmark of clean, professional JavaScript.
Side-by-Side Comparison
| Feature | map() | forEach() |
|---|---|---|
| Returns | New array (same length) | undefined |
| Chainable | ✅ Yes — .map().filter().reduce() | ❌ No — returns undefined |
| Purpose | Transform every element | Run side effects |
| Mutates original | ❌ No | ❌ No (unless you explicitly mutate) |
| Skips sparse holes | ✅ Yes | ✅ Yes |
| break/continue | ❌ Not possible | ❌ Not possible |
| Can stop early | ❌ No | ❌ No |
| Use in React | ✅ Common (render lists) | ❌ Rarely |
Code Proof: Return Value Difference
map result: [2, 4, 6] forEach result: undefined
Code Proof: Chaining
"₹250, ₹400"
When to Use Each
Use map() — when you need a transformed array
[200, 400, 600]
["Alice", "Bob"]
[{id: 1, name: "Alice"}, {id: 2, name: "Bob"}]Use forEach() — when you have side effects
Fruit: Apple Fruit: Banana Fruit: Cherry [2, 4, 6]
The Common Mistake: Using map() for Side Effects
1 2 3 [undefined, undefined, undefined] 1 2 3
Performance Comparison
map: ~15ms forEach: ~20ms
Note:map()is often slightly faster because it pre-allocates the result array. Usemap()for transformations — it's both idiomatic and slightly more efficient.
React: Why map() Wins
In React (and other frameworks), you must return JSX from a callback to render a list. forEach() returns undefined which renders nothing.
"<a key="0">Home</a> | <a key="1">About</a> | <a key="2">Contact</a>" undefined
Real World Decision Guide
Do you need the result?
YES → Use map()
NO → Use forEach()
Do you need to chain more operations?
YES → Use map()
NO → Use forEach()
Are you rendering a list in React?
YES → Use map()
NO → Could be either
Are you logging, saving, or updating UI?
YES → Use forEach()
NO → Probably map()Cheat Sheet
Need a new array? → map()
Just looping? → forEach()
Chainable pipeline? → map()
Side effects (log/save)? → forEach()
React list rendering? → map()
Collecting to external arr? → forEach() with push, or map()
map() = transform every element → new array
forEach() = iterate every element → undefined (no return)Interview Questions
Q1. What is the main difference between map() and forEach()?
map() returns a new array of transformed values and is chainable. forEach() returns undefined and is used for running side effects (logging, DOM updates, saving to DB). They both iterate every element but serve different purposes.
Q2. Can you chain methods after forEach()?
No. forEach() returns undefined, so calling .map() or .filter() after it throws a TypeError. Only use chaining with map(), filter(), reduce(), etc.
Q3. Which is faster — map() or forEach()?
map() is generally marginally faster because it pre-allocates the output array. But for real applications, the difference is negligible. Choose based on intent, not micro-performance.
Q4. Can you use forEach() to build a new array?
Technically yes — you push to an external array inside the callback. But it's cleaner to use map() instead. forEach() is for side effects, map() is for transformation.
Q5. Why does map() return [undefined, undefined, undefined] sometimes?
When the callback doesn't have a return statement (especially with curly brace syntax), the function returns undefined, which map() stores as the element. Fix: ensure every code path in the callback returns a value.
Q6. In React, why do we use map() and not forEach()?
React's render function needs to return an array of JSX elements. map() collects those return values into an array that React can render. forEach() returns undefined — React can't render undefined as a list.
Q7. Does either map() or forEach() mutate the original array?
Neither mutates the original array by default. However, if your callback mutates the objects inside the array (e.g., obj.count++), those mutations happen because objects are references — but that's not the method's doing.
Key Takeaways
map()returns a new array —forEach()returnsundefined- Use
map()for transformations,forEach()for side effects - Only
map()supports method chaining - In React, always use
map()to render lists map()with a forgottenreturnproduces[undefined, ...]— a very common bug- Performance difference is negligible in practice — choose based on semantic meaning
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for JavaScript map() vs forEach() - Key Differences with Examples.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this JavaScript Master Course topic.
Search Terms
javascript-complete-guide, javascript master course, javascript, complete, guide, arrays, map, foreach
Related JavaScript Master Course Topics