The map() method transforms every element with a callback and returns a new array of the same length. This tutorial covers syntax, five examples, comparison with forEach(), object extraction, type conversion, and when to reach for flatMap() instead.
01
Syntax
map(fn)
02
New array
Same length
03
Immutable
Source unchanged
04
Callback
Return value
05
Index arg
Optional use
06
ES5
Wide support
Fundamentals
Introduction
When you need to convert each item in a list—squaring numbers, pulling names from objects, or formatting strings—map() is the standard tool. You describe the transformation once; JavaScript applies it to every element and hands you a fresh array.
Unlike a manual for loop with push, map() expresses intent clearly: “give me a new array where each value is the result of this function.”
Concept
Understanding the map() Method
array.map(callback, thisArg?) invokes callback(element, index, array) for each index that exists. Whatever the callback returns becomes the value at that position in the new array.
The output array always has the same length as the source (unless you are working with sparse arrays with holes—holes stay holes). The original array is never replaced or reordered by map() itself.
💡
Beginner Tip
Think of map() as a photocopier that edits each page: same number of pages, new content. Use forEach() when you only want to do something (log, save) without building a new list.
The second argument is the zero-based index. Add 1 when you want human-friendly numbering.
Applications
🚀 Common Use Cases
Math on numbers — scale, round, or format values.
Pluck fields — extract ids, names, or prices from objects.
Parse input — convert strings to numbers or dates.
Render lists — build HTML strings or React elements (in UI code).
Normalize data — map API rows to a consistent shape.
Chain transforms — .map(fn).filter(fn) pipelines.
🧠 How map() Runs
1
Create empty result
Engine prepares a new array with the same length.
Setup
2
Loop each index
Skip holes in sparse arrays; call callback for existing elements.
Iterate
3
Store return value
Callback result goes at the same index in the new array.
Transform
4
📦
Return new array
Original array untouched; you get the mapped copy.
Important
📝 Notes
Always returns a new array (same length as source for dense arrays).
Callback must return a value; missing returns become undefined.
Does not mutate the original array structure.
For side effects only, prefer forEach().
If callback returns nested arrays you want flattened, use flatMap().
Async callbacks are not awaited—use Promise.all(arr.map(async ...)) for parallel async work.
ES5—excellent support including IE9+.
Compatibility
Browser & Runtime Support
Array.prototype.map() has been available since ES5 (2009). It is one of the core functional array methods every JavaScript developer uses daily.
✓ Baseline · ES5
Array.prototype.map()
Supported in Chrome 1+, Firefox 1.5+, Safari 3+, Edge (all versions), IE 9+, and all modern Node.js versions.
99%Universal support
Google ChromeSupported · Desktop & Mobile
Full support
Mozilla FirefoxSupported · Desktop & Mobile
Full support
Apple SafariSupported · macOS & iOS
Full support
Microsoft EdgeSupported · Chromium
Full support
Internet ExplorerNo native support · Use a polyfill
Polyfill
OperaSupported · Modern versions
Full support
Samsung InternetSupported · Android
Full support
BunSupported · JavaScript runtime
Supported
DenoSupported · JavaScript runtime
Supported
Node.jsSupported · Server runtime
Supported
Android WebViewSupported · Modern WebView
Full support
Array.map()Excellent
Bottom line: Safe to use everywhere except very old IE8 environments. No polyfill needed for modern projects.
Wrap Up
Conclusion
map() is the go-to method when you want a new array derived from an existing one. Describe the per-element transformation, collect the results, and keep your source data unchanged.
Next, learn pop() to remove and return the last element from an array.
Assign the result to a new variable instead of mutating the source
Extract reusable callbacks when logic grows
Use map for 1:1 transforms; filter to subset
Chain map with filter or reduce for pipelines
❌ Don’t
Use map when you only need to log (use forEach)
Push inside map to a side array—just return the value
Forget that nested arrays need flatMap to flatten
Assume async callbacks run sequentially inside map
Mutate shared state heavily inside the callback without reason
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about Array.map()
Transform every element and get a new array back.
5
Core concepts
📝01
Syntax
arr.map(fn)
API
📦02
New array
Same length.
Output
🔄03
Immutable
Source safe.
Pattern
👤04
Objects
Pluck fields.
Data
📋05
vs forEach
Returns data.
Compare
❓ Frequently Asked Questions
map() calls a callback on every element and collects each return value into a new array. The original array is not changed.
No. map() always returns a new array. If your callback mutates objects inside the source array, those objects can still change—but the array structure itself is untouched.
map() returns a new transformed array. forEach() returns undefined and is meant for side effects like logging or updating external variables.
The corresponding slot in the new array becomes undefined. Always return a value from your callback when you need meaningful results.
Yes. The callback receives (element, index, array). Use index when the position matters, such as numbering items or pairing with another array.
map() is ES5 (2009). It works in all modern browsers and has been supported in Internet Explorer 9+.
Did you know?
You can pass a function reference directly: ["a", "b"].map(String.toUpperCase) works because toUpperCase receives each element as this. For clarity, arrow wrappers like (s) => s.toUpperCase() are more common in tutorials.