JavaScript Notes
Master JavaScript array map() method: syntax, transformations, mapping objects, chaining, React rendering, vs forEach, common mistakes, real-world use cases, interview Q&A.
The map() method creates a new array by transforming every element using a callback function. It's the go-to tool for data transformation in JavaScript and is essential for React rendering.
Basic Usage
[2, 4, 6, 8, 10] [1, 4, 9, 16, 25] ["1", "2", "3", "4", "5"] [1, 2, 3, 4, 5]
Using Index in map()
["1. apple", "2. banana", "3. cherry"]
[
{id: 1, name: "Alice", email: "alice@example.com"},
{id: 2, name: "Bob", email: "bob@example.com"},
{id: 3, name: "Carol", email: "carol@example.com"}
]Mapping Arrays of Objects
["Laptop", "Phone", "Tablet"]
{name: "Laptop", price: 999, tax: 0.1, total: 1098.9}
[{name: "Laptop", price: 999}, {name: "Phone", price: 699}, {name: "Tablet", price: 499}]map() with Destructuring
[5, 0, 13] [3, 7, 11]
Chaining map() with Other Methods
[
{product: "Laptop", total: 1998},
{product: "Monitor", total: 1350},
{product: "Mouse", total: 250}
]Real World Use Cases
<li key="0">Home</li>
<li key="1">About</li>
<li key="2">Contact</li>
[{fullName: "Alice Smith", age: 28}, {fullName: "Bob Jones", age: 35}]
With 18% GST: [118, 295, 448.4]Common Mistakes
| ❌ Wrong | ✅ Correct | Why |
|---|---|---|
arr.map(n => { n * 2 }) | arr.map(n => n * 2) or arr.map(n => { return n * 2; }) | Curly braces need explicit return |
Using map() just for console.log | Use forEach() instead | map() is for transformation — side effects only = forEach() |
arr.map(parseInt) expecting [1, 2, 3] | arr.map(n => parseInt(n)) | parseInt receives (value, index, array) — the second arg (index) acts as radix |
Expecting map() to filter elements | Chain .filter() before .map() | map() always returns same-length array; it can't skip elements |
| Mutating original objects inside map | Return a new object: { ...item, updated: true } | Mutations inside map cause hard-to-trace bugs |
map() vs forEach()
| Feature | map() | forEach() |
|---|---|---|
| Returns | New array | undefined |
| Purpose | Transform values | Run side effects |
| Chainable | ✅ Yes | ❌ No |
| Skips sparse holes | ✅ Yes | ✅ Yes |
| Use when | You need a new array | You just want to loop |
[2, 4, 6] 1 2 3 undefined
Cheat Sheet
Interview Questions
Q1. What does map() return?
A new array of the same length as the original. Each element is the return value of the callback for that position.
Q2. Does map() mutate the original array?
No. map() never modifies the original array. It creates and returns a completely new array.
Q3. What happens if you forget the return statement in a map() callback?
Each element becomes undefined. Example: [1,2,3].map(n => { n * 2 }) returns [undefined, undefined, undefined].
Q4. What is the difference between map() and forEach()?
map() returns a new array with transformed values — use it when you need a result. forEach() returns undefined — use it only for side effects like logging or updating external state.
Q5. Why does [1, 2, 3].map(parseInt) not work as expected?
parseInt accepts (string, radix). map passes (element, index, array), so the index becomes the radix. parseInt("2", 1) returns NaN. Fix: [1, 2, 3].map(n => parseInt(n)).
Q6. Can map() remove elements?
No. map() always returns an array of the same length. Elements you "skip" become undefined. To filter and transform, use .filter().map().
Q7. How do you use map() in React?
Key Takeaways
map()always returns a new array of the same length- The callback's return value becomes the element in the new array
- Forgetting
returninside curly braces fills the new array withundefined - Use
map()for transformation,forEach()for side effects,filter()for removing elements map()is chainable —.filter().map().reduce()pipelines are powerful- Never use
map()just to callconsole.log— that'sforEach()'s job
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for JavaScript Array map() Method - Complete Tutorial 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, javascript array map() method - complete tutorial with examples
Related JavaScript Master Course Topics