JavaScript map Method
Table of Contents + −
In the previous lesson, we learned about the array methods JavaScript gives us. Now let’s look closely at map, the method you reach for whenever you want to turn one array into a new array.
🗺️ What is the map Method?
The map method builds a brand new array by transforming each item in the original array. You give map a function, and map calls that function once for every item. Whatever the function returns becomes the matching item in the new array.
The function you pass to map is called a callback, because map calls it back for you on each item. It pairs naturally with arrow functions, which keep the syntax short.
Here is the simplest example, doubling each number:
const numbers = [1, 2, 3, 4];
const doubled = numbers.map((number) => number * 2);
console.log(doubled); // [2, 4, 6, 8]Let’s walk through what each line does:
const numbers = [1, 2, 3, 4]is the source array we want to transform.numbers.map((number) => number * 2)runs the arrow function once per item:numberis1and returns2, thennumberis2and returns4, and so on.mapcollects those returned values into the new array stored indoubled.console.log(doubled)prints[2, 4, 6, 8], the doubled values in order.
🔒 map Never Changes the Original
The map method always leaves your original array untouched and hands you a fresh array instead.
const numbers = [1, 2, 3, 4];
const doubled = numbers.map((number) => number * 2);
console.log(doubled); // [2, 4, 6, 8]console.log(numbers); // [1, 2, 3, 4] → still the sameLet’s compare the two log lines:
console.log(doubled)shows the new array[2, 4, 6, 8]thatmapbuilt.console.log(numbers)shows[1, 2, 3, 4], proving the original array was not changed.
Because the original stays exactly as it was, map is safe and predictable, which is why it is so popular for working with data.
Same length, new values
The array map returns always has the same number of items as the original.
One item goes in, one item comes out. If you want fewer items, you want
filter instead.
💵 A Practical Example
In real code you often transform a list into something ready to display. Here we turn an array of price numbers into formatted price strings:
const prices = [9.5, 20, 4.99];
const labels = prices.map((price) => `$${price.toFixed(2)}`);
console.log(labels); // ["$9.50", "$20.00", "$4.99"]Let’s break down the transformation:
pricesholds plain numbers that aren’t ready to show to a user.- For each
price, the callback returns a template string with a$andprice.toFixed(2), which forces exactly two decimal places. labelsends up with the display-ready strings, whilepricesis left untouched.
The map method is just as common with arrays of objects. Here we pull one property out of each object:
const users = [ { id: 1, name: "Alex" }, { id: 2, name: "Sam" }, { id: 3, name: "Jordan" },];
const names = users.map((user) => user.name);
console.log(names); // ["Alex", "Sam", "Jordan"]Let’s trace how the objects become names:
usersis an array of objects, each with anidand aname.- The callback receives one
userat a time and returns justuser.name. namesis a clean array of the name strings, dropping theidand the surrounding object.
🔢 The Callback Receives the Index Too
The callback can take a second value, the index, which tells you the position of the current item. The index starts at 0.
const fruits = ["apple", "banana", "cherry"];
const numbered = fruits.map((fruit, index) => `${index + 1}. ${fruit}`);
console.log(numbered); // ["1. apple", "2. banana", "3. cherry"]Let’s see how the index shapes the result:
- The callback now takes two parameters:
fruitis the current item andindexis its position. indexis0,1, and2across the three passes, so we add1to make the list read1,2,3instead of starting at zero.- The template string combines the number and the fruit, giving
numberedthe labeled strings.
You only need to name the index parameter when you actually use it.
⚠️ Common Mistakes to Avoid
| Mistake | Problem | Solution |
| Forgetting to return from the callback | You get an array full of undefined | Make sure the callback returns a value for each item |
Expecting map to change the original array | The original stays the same and your change seems lost | Use the new array that map returns |
Using map when you only want to do something with each item | You build an array you never use | Use forEach when you do not need a new array |
The first mistake is the most common one. When you write a callback with curly braces, you must add the return keyword yourself:
const numbers = [1, 2, 3];
// ❌ Curly braces with no return → [undefined, undefined, undefined]const wrong = numbers.map((number) => { number * 2;});
// ✅ Add return, or skip the braces so the value returns automaticallyconst right = numbers.map((number) => { return number * 2;});
console.log(wrong); // [undefined, undefined, undefined]console.log(right); // [2, 4, 6]Let’s see what differs between the two callbacks:
- In the ❌
wrongversion, the curly braces make a function body, andnumber * 2is computed but never returned. With noreturn, the callback hands backundefinedeach time, sowrongis[undefined, undefined, undefined]. - In the ✅
rightversion, the explicitreturn number * 2sends the value back, sorightis[2, 4, 6]. - The fix is to add
returnwhenever you use curly braces, or drop the braces so the arrow function returns the value automatically.
map when you transform, forEach when you act
Use map when you want a new array of transformed values. If you just want to
do something with each item, like printing it, reach for forEach instead.
🔧 Try It Yourself!
- Create an array of numbers and use
mapto triple each one. - Make an array of names and use
mapto turn each name into a greeting like"Hello, Alex". - Create an array of objects with a
priceproperty and usemapto pull out just the prices. - Use the
indexparameter to build a numbered list from an array of words. - Write a callback with curly braces but no
return, run it, and confirm you get an array ofundefined.
🧩 What You’ve Learned
- ✅
mapbuilds a new array by transforming each item - ✅ The callback returns the new value for each item
- ✅
mapnever changes the original array - ✅ The new array always has the same length as the original
- ✅ The callback can also receive the
indexof each item - ✅ Forgetting to
returnfrom the callback gives an array ofundefined
🚀 What’s Next?
Now that you can transform an array, let’s learn how to keep only the items you want. Let’s continue to filter.