JavaScript Array map() Method

Beginner
⏱️ 9 min read
📚 Updated: Jul 2026
🎯 5 Examples
🚀 5 Try-it labs
Transform

What You’ll Learn

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

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.”

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.

📝 Syntax

General form of Array.prototype.map:

JavaScript
const newArray = array.map(callback(element, index, array), thisArg)

Parameters

  • callback — function run for each element; its return value is stored in the new array.
  • thisArg — optional; value of this inside the callback (rarely needed with arrow functions).

Callback arguments

  • element — current item being processed.
  • index — position (0-based).
  • array — the array map() was called on.

Return value

  • A new array with transformed values.
  • Original array unchanged.

Common patterns

  • arr.map((x) => x * 2) — numeric transform.
  • users.map((u) => u.name) — pluck a property.
  • strings.map(Number) — pass a built-in function.
  • arr.map((v, i) => i + ": " + v) — use index.

⚡ Quick Reference

GoalCode
Transform each elementarr.map(fn)
Return typeNew array (same length)
Side effects onlyUse forEach instead
Map + flatten one levelflatMap (ES2019)
Mutates original?No

📋 map() vs forEach() vs flatMap() vs filter()

Pick the method that matches whether you need a new array, side effects, flattening, or subset selection.

map
new array

1:1 transform

forEach
undefined

Side effects

flatMap
map + flat(1)

Nested results

filter
subset

Keep some items

Examples Gallery

Open DevTools Console (F12) or use Try-it labs. Example N maps to ?tryit=N.

📚 Getting Started

Basic numeric and object transforms.

Example 1 — Square Each Number

Apply a math operation to every element and collect the results.

JavaScript
const numbers = [1, 2, 3, 4, 5];

const squared = numbers.map((num) => num ** 2);

console.log(squared);
// [1, 4, 9, 16, 25]

console.log(numbers);
// [1, 2, 3, 4, 5] — unchanged
Try It Yourself

How It Works

Each callback return value lands at the same index in the new array. The source array stays intact.

Example 2 — Extract a Property from Objects

Pull name from an array of user records.

JavaScript
const users = [
  { id: 1, name: "Alice" },
  { id: 2, name: "Bob" },
  { id: 3, name: "Charlie" }
];

const names = users.map((user) => user.name);

console.log(names);
// ["Alice", "Bob", "Charlie"]
Try It Yourself

How It Works

map() is ideal for shaping API data—objects in, simple values out.

📈 Practical Patterns

Type conversion, return values, and index usage.

Example 3 — Convert Strings to Numbers

Pass Number directly as the callback—no wrapper needed.

JavaScript
const stringNumbers = ["1", "2", "3"];

const integers = stringNumbers.map(Number);

console.log(integers);
// [1, 2, 3]

console.log(typeof integers[0]);
// "number"
Try It Yourself

How It Works

Any function with the right signature works as a callback. Number receives each string and returns a number.

Example 4 — map() Returns a New Array; forEach() Does Not

See why you chain or assign from map, not forEach.

JavaScript
const nums = [1, 2, 3];

const fromMap = nums.map((n) => n * 10);
const fromForEach = nums.forEach((n) => n * 10);

console.log("map:", fromMap);
// [10, 20, 30]

console.log("forEach:", fromForEach);
// undefined
Try It Yourself

How It Works

Need transformed data? Use map. Need to log or mutate external state? Use forEach.

Example 5 — Use the Index Parameter

Build numbered labels by combining index and value in the callback.

JavaScript
const fruits = ["apple", "banana", "cherry"];

const numbered = fruits.map((fruit, index) => {
  return (index + 1) + ". " + fruit;
});

console.log(numbered);
// ["1. apple", "2. banana", "3. cherry"]
Try It Yourself

How It Works

The second argument is the zero-based index. Add 1 when you want human-friendly numbering.

🚀 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.

📝 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+.

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 Chrome Supported · Desktop & Mobile
Full support
Mozilla Firefox Supported · Desktop & Mobile
Full support
Apple Safari Supported · macOS & iOS
Full support
Microsoft Edge Supported · Chromium
Full support
Internet Explorer No native support · Use a polyfill
Polyfill
Opera Supported · Modern versions
Full support
Samsung Internet Supported · Android
Full support
Bun Supported · JavaScript runtime
Supported
Deno Supported · JavaScript runtime
Supported
Node.js Supported · Server runtime
Supported
Android WebView Supported · Modern WebView
Full support
Array.map() Excellent

Bottom line: Safe to use everywhere except very old IE8 environments. No polyfill needed for modern projects.

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.

💡 Best Practices

✅ Do

  • Return a value from every callback invocation
  • 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

Key Takeaways

Knowledge Unlocked

Five things to remember about Array.map()

Transform every element and get a new array back.

5
Core concepts
📦 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.

Continue to pop()

Learn how to remove and return the last element from an array.

pop() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

6 people found this page helpful