Master Code
On The Go

Learn. Practice. Build.

Home › Code Snippets › JavaScript › Reference › map()

JavaScript Reference

Array.prototype.map()

Creates a new array by transforming every element with a callback function.

Last updated: October 2026

map() transforms every element in an array and returns a new array. Use it when you need to change each item without mutating the original.

Full definition: Array.prototype.map() creates a new array by calling a function on every element of the original array. It doesn't modify the original array.

🎬 Live Example

Five common map patterns. Click any button to see the output.

Input:

                
Output:

            

💡 Every example returns a new array. The input is never modified.

📝

Basic Syntax

array.map(callback(element, index, array))

// Returns a new array of the same length

The callback receives three arguments, but you usually only need the first one.

🎯 Syntax & Parameters

Parameter Type Description
callbackFunctionRuns for each element. Returns the new value.
elementanyThe current element being processed.
indexnumberThe index of the current element.
arrayArrayThe original array map was called on.
1

Transform Numbers

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

const doubled = numbers.map(n => n * 2);
// [2, 4, 6, 8, 10]

const squared = numbers.map(n => n ** 2);
// [1, 4, 9, 16, 25]

// Original is unchanged
console.log(numbers); // [1, 2, 3, 4, 5]
2

Extract Properties from Objects

const users = [
    { name: 'Alice', age: 30 },
    { name: 'Bob', age: 25 },
    { name: 'Carol', age: 35 }
];

const names = users.map(u => u.name);
// ['Alice', 'Bob', 'Carol']

const ages = users.map(u => u.age);
// [30, 25, 35]
3

Using the Index Argument

const letters = ['a', 'b', 'c'];

const withIndex = letters.map((letter, i) => `${i}: ${letter}`);
// ['0: a', '1: b', '2: c']

// Build list items
const htmlItems = letters.map((letter, i) =>
    `<li data-index="${i}">${letter}</li>`
);
4

Chaining map with Other Methods

const numbers = [1, 2, 3, 4, 5, 6, 7, 8];

const result = numbers
    .map(n => n ** 2)      // [1, 4, 9, 16, 25, 36, 49, 64]
    .filter(n => n > 10);  // [16, 25, 36, 49, 64]

// With reduce
const total = numbers
    .map(n => n * 2)
    .reduce((sum, n) => sum + n, 0);
// 72

💡 Chain tip: map → filter runs the filter only after all elements are mapped. filter → map runs map only on the filtered elements. Choose the order that makes the most sense for your logic.

5

Common Real-World Example — Rendering Lists

// React rendering
function UserList({ users }) {
    return (
        <ul>
            {users.map(user => (
                <li key={user.id}>{user.name}</li>
            ))}
        </ul>
    );
}

// Vanilla JS
const items = products.map(p =>
    `<div class="card">
        <h3>${p.name}</h3>
        <p>$${p.price}</p>
    </div>`
).join('');

document.querySelector('#products').innerHTML = items;

🎯 map vs forEach vs filter

Method Returns Use when
mapNew array, same lengthYou want to transform each element
filterNew array, subsetYou want to keep only matching items
forEachundefinedYou want a side effect (logging, updating)
reduceSingle valueYou want to combine into one value
findElement or undefinedYou want the first matching item

⚠️ Common Mistakes

❓ Frequently Asked Questions

What does Array.map() do in JavaScript?

Array.map() creates a new array by calling a function on each element of the original array. It returns a new array of the same length, without modifying the original array.

What is the difference between map and forEach?

map returns a new array with the results of the callback. forEach returns undefined and is used only for side effects like logging or updating external state. Use map when you need the transformed array.

Can I chain map with filter?

Yes. Both return new arrays, so you can chain them: arr.map(x => x * 2).filter(x => x > 10). The array passes through each method in order.

Does map mutate the original array?

No. map returns a new array. The original array remains unchanged. Any mutations to objects inside the array will still affect both, because the objects are shared by reference.

What are the parameters of the map callback?

The callback receives three arguments: the current element, the index of that element, and the original array. Most code only uses the first argument.

🎯 What's Next?

← All JavaScript Reference