Last modified: Jul 27, 2026
JavaScript Map Array Guide
The JavaScript map array method is a powerful tool. It creates a new array. It does not change the original array. It transforms every item in an array. This guide explains everything you need to know.
You will learn the syntax. You will see real examples. You will understand when to use it. This is perfect for beginners and experienced developers.
What is the Map Method?
The map() method calls a function for each array element. It returns a new array. The original array stays the same. This is a key feature of functional programming in JavaScript.
Think of it as a factory line. Each item goes in. A transformation happens. A new item comes out. The old items are untouched.
It is one of the most used JavaScript Array Methods.
Syntax of Map
The basic syntax is simple. You call it on an array. You pass a callback function. The function runs for every element.
let newArray = originalArray.map(function(currentValue, index, arr) {
// Return the transformed item
});
The callback function takes three arguments:
- currentValue – The current element being processed.
- index (optional) – The index of the current element.
- arr (optional) – The original array.
Only currentValue is required. The others are optional. Use them when you need extra context.
Basic Example
Let's start with a simple example. We have an array of numbers. We want to double each number.
// Original array
const numbers = [1, 2, 3, 4, 5];
// Use map to double each number
const doubled = numbers.map(function(num) {
return num * 2;
});
console.log(doubled); // Output: [2, 4, 6, 8, 10]
console.log(numbers); // Output: [1, 2, 3, 4, 5] (original unchanged)
[2, 4, 6, 8, 10]
[1, 2, 3, 4, 5]
Notice the original array is unchanged. The new array holds the doubled values. This is clean and predictable.
Using Arrow Functions
Modern JavaScript uses arrow functions. They make the code shorter. Here is the same example with an arrow function.
const numbers = [1, 2, 3, 4, 5];
const doubled = numbers.map(num => num * 2);
console.log(doubled); // [2, 4, 6, 8, 10]
Arrow functions are great for simple transformations. They reduce boilerplate code.
Transforming Objects
The map() method works well with arrays of objects. You can extract or modify properties.
const users = [
{ name: 'Alice', age: 25 },
{ name: 'Bob', age: 30 },
{ name: 'Charlie', age: 35 }
];
// Extract only the names
const names = users.map(user => user.name);
console.log(names); // ['Alice', 'Bob', 'Charlie']
// Add a new property
const usersWithRole = users.map(user => ({
...user,
role: 'member'
}));
console.log(usersWithRole);
// [
// { name: 'Alice', age: 25, role: 'member' },
// { name: 'Bob', age: 30, role: 'member' },
// { name: 'Charlie', age: 35, role: 'member' }
// ]
['Alice', 'Bob', 'Charlie']
[
{ name: 'Alice', age: 25, role: 'member' },
{ name: 'Bob', age: 30, role: 'member' },
{ name: 'Charlie', age: 35, role: 'member' }
]
This is very useful when working with API data. You can reshape objects easily.
Using the Index Parameter
The index parameter gives you the position of each element. This is helpful for creating numbered lists.
const fruits = ['apple', 'banana', 'cherry'];
const labeledFruits = fruits.map((fruit, index) => {
return `${index + 1}. ${fruit}`;
});
console.log(labeledFruits);
// ['1. apple', '2. banana', '3. cherry']
['1. apple', '2. banana', '3. cherry']
You can also use the index for conditional logic. For example, skip or modify certain positions.
Chaining Map with Other Methods
You can chain map() with other array methods. This makes data processing pipelines powerful.
const numbers = [1, 2, 3, 4, 5, 6];
// Filter even numbers, then double them
const result = numbers
.filter(num => num % 2 === 0)
.map(num => num * 2);
console.log(result); // [4, 8, 12]
[4, 8, 12]
This is a common pattern. First, filter unwanted items. Then, transform the remaining ones.
Common Mistakes
Beginners often make these mistakes. Avoid them for cleaner code.
Mistake 1: Forgetting to return a value. If you don't return anything, the new array will have undefined values.
const numbers = [1, 2, 3];
const wrong = numbers.map(num => {
num * 2; // No return statement!
});
console.log(wrong); // [undefined, undefined, undefined]
[undefined, undefined, undefined]
Mistake 2: Mutating the original array. While map() does not mutate the array, you can accidentally modify objects inside it.
const users = [{ name: 'Alice' }];
const bad = users.map(user => {
user.name = 'Bob'; // Mutates the original object!
return user;
});
console.log(users); // [{ name: 'Bob' }] (original changed)
Always create new objects inside map() to avoid side effects.
Map vs ForEach
Many beginners confuse map() and forEach(). The key difference is that map() returns a new array. forEach() does not return anything.
Use map() when you need a transformed array. Use forEach() when you want to perform an action, like logging.
Practical Use Cases
Here are common scenarios where map() shines.
- Formatting data for display: Convert dates or currencies.
- Extracting IDs from an array of objects.
- Rendering lists in React or other frameworks.
- Converting data types, like strings to numbers.
For more on handling arrays, check our JavaScript Array Variables Guide.
Performance Considerations
For small arrays, performance is not an issue. For very large arrays, map() is efficient. It runs in O(n) time.
However, avoid chaining many methods on huge arrays. This creates multiple intermediate arrays. Use reduce() for complex operations on large datasets.
Conclusion
The JavaScript map() array method is essential. It transforms data cleanly and safely. It does not mutate the original array. It returns a new array with the results.
Remember to always return a value. Use arrow functions for simplicity. Chain it with other methods for powerful pipelines. Practice with different data types. You will soon master this method.
Start using map() today. It will make your code more readable and functional. Happy coding!