JavaScript Array map() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Array

Definition

Array.prototype.map(callback) builds a new array by calling callback on every element and collecting each return value. Length stays the same; the source array is not mutated. For side effects only, prefer forEach(). To keep some items and drop others, use filter().

Remember
array.map(callback)
array.map((element, index, array) => ...)

[1, 2, 3].map(n => n * 2)     → [2, 4, 6]
["a", "b"].map(s => s.toUpperCase()) → ["A", "B"]

returns a new array (same length)
original array unchanged
always return a value from the callback

Syntax

Call array.map. Start with this line.

JavaScript
array.map(callback)
CallWhat it does
array.map(callback)Primary signature.
array.map(...)Call with the arguments your case needs.

Built into JavaScript. Call map(...) on an array instance — no import required.

Library declarations
JavaScript
array.map(callback)

Parameters

ParameterDescription
callbackRequired. Runs once per element; return the new value
indexOptional. Current index (0-based)

Return Value

Return ValueDescription
*New array of the same length

JavaScript Examples

Example 1 — Transform Numbers

Return a new value for every element.

JavaScript
const nums = [1, 2, 3, 4];
const doubled = nums.map(n => n * 2);

console.log(JSON.stringify(doubled));  // [2,4,6,8]
console.log(JSON.stringify(nums));     // [1,2,3,4] unchanged
Try It Yourself

How It Works

  1. Callback runs per item. 1→2, 2→4, 3→6, 4→8.
  2. New array. Results are collected into doubled.
  3. Same length. Four inputs → four outputs.
  4. Source safe. nums is still [1, 2, 3, 4].

Example 2 — Extract Object Fields

Pull one property from each object into a simpler array.

JavaScript
const users = [
  { id: 1, name: "Ada" },

  { id: 2, name: "Grace" }
];

const names = users.map(u => u.name);
const labels = users.map((u, i) => (i + 1) + ". " + u.name);

console.log(JSON.stringify(names));   // ["Ada","Grace"]
console.log(JSON.stringify(labels));  // ["1. Ada","2. Grace"]
Try It Yourself

How It Works

  1. Pick a field. u.name becomes each new element.
  2. Use the index. Second map numbers items with i + 1.
  3. Objects stay. users is unchanged; you only built new arrays.
  4. Common pattern. Great for dropdown labels or ID lists.

Example 3 — map vs forEach

map returns data; forEach returns undefined.

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

const mapped = nums.map(n => n * 10);
const each = nums.forEach(n => n * 10);

console.log(JSON.stringify(mapped));  // [10,20,30]
console.log(String(each));            // undefined
Try It Yourself

How It Works

  1. map collects. Each return value becomes part of mapped.
  2. forEach discards. Even if you return a value, the method result is undefined.
  3. Pick by intent. Need a new array → map; only side effects → forEach.
  4. Avoid the trap. Do not assign forEach to a variable expecting data.

Browser & Runtime Support

Array.prototype.map() is part of ES5 (2009). It has been available in evergreen browsers and Node.js for a long time.

✓ Baseline · ES5

Array.prototype.map()

Supported in all modern browsers, Internet Explorer 9+, and every current Node.js release.

99% Browser 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 everywhere. Prefer map() when you need a new array; use forEach() only for side effects.

Frequently Asked Questions

map() calls a callback on every element and collects each return value into a new array of the same length. The original array is not changed.
No. map() always returns a new array. If your callback mutates objects inside the source array, those shared objects can still change — but the array shell is new.
map() returns a new transformed array. forEach() returns undefined and is meant for side effects like logging.
That slot in the new array becomes undefined. Always return a value when you need meaningful results.
Yes. The callback receives (element, index, array). Use index when the position matters.
map() is ES5 (2009). It works in all modern browsers and has been supported in Internet Explorer 9+.

Next: pop()

Remove and return the last element of 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.

11 people found this page helpful