undefined

JavaScript ES6+ · ✓ verified by execution on 2026-08-12T19:46:28.915Z

Introduction

In JavaScript, arrays are incredibly powerful data structures, and the language provides a variety of built-in methods to manipulate them. When working with arrays, one of the most common tasks is transforming every item in an array into something else, producing a brand new array with the transformed elements.

For instance, you might want to double every number in an array, extract the names from a list of user objects, or format a list of prices into currency strings. While you could achieve this using a standard for loop or the forEach() method by manually pushing items into a new array, JavaScript provides a more elegant and expressive way to do this: the map() method.

The map() method of Array instances creates a new array populated with the results of calling a provided function on every element in the calling array.

Mapping elements 1-to-1
map() is one-to-one: every element of the input produces exactly one element of the output, so the new array is always the same length as the old one.

Basic Usage

The map() method is a higher-order function that takes a callback function as its argument. This callback function is executed once for each element in the array. Whatever the callback function returns is placed into the new array at the exact same index. This declarative style of programming allows you to focus on the transformation logic itself rather than the mechanics of iterating over the array.

javascript
const numbers = [1, 2, 3];
const doubled = numbers.map(num => num * 2);
console.log(doubled);
Output
[ 2, 4, 6 ]

Extracting Data

One of the most practical and frequent use cases for map() is extracting a specific piece of data from a collection of objects. In modern web development, you often receive data from an API as an array of objects. If you only need a specific property from each object—for example, you want an array containing only the names of users to display in a dropdown list—map() is the perfect tool for the job.

javascript
const users = [{ name: 'Alice', age: 25 }, { name: 'Bob', age: 30 }];
const names = users.map(user => user.name);
console.log(names);
Output
[ 'Alice', 'Bob' ]

Pure Functions and Immutability

map() does not mutate the array on which it is called.

It is important to understand that map() operates in a purely functional way. It creates and returns a completely new array, leaving the original array exactly as it was. This immutability is a core concept in modern JavaScript (especially when working with libraries like React). It prevents unintended side effects that can occur when you mutate data directly.

javascript
const words = ['hello', 'world'];
const loudWords = words.map(word => word.toUpperCase());
console.log(loudWords);
Output
[ 'HELLO', 'WORLD' ]

The Missing Return Pitfall

A common pitfall is forgetting to return a value from the callback function; if no value is returned, the new array will be filled with undefined.

A very common mistake when writing map() callbacks, especially when switching from concise arrow functions to block-body arrow functions (the ones with curly braces), is forgetting to explicitly return the value. If a function doesn’t return anything, it implicitly returns undefined. Since map() relies on the return value to build the new array, you’ll end up with an array full of undefined values.

Predict the output javascript

Read the code. What exactly will it print? Commit to an answer before you look.

const nums = [1, 2];
const result = nums.map(n => { n * 2; });
console.log(result);
Output
[ undefined, undefined ]

Accessing the Index

The callback function accepts three arguments: currentValue, index, and array.

Just like forEach(), the callback function you provide to map() actually receives three arguments: the current element being processed, the index of that element, and the original array itself. While you often only need the first argument, the index is essential when the transformation depends on the item’s position in the list.

javascript
const items = ['apple', 'banana'];
const numbered = items.map((item, index) => index + ': ' + item);
console.log(numbered);
Output
[ '0: apple', '1: banana' ]

Step-by-Step Visualization

javascript · visualize
const nums = [1, 2, 3];
const doubled = nums.map(n => n * 2);
console.log(doubled);

Handling Empty Slots

map() does not call the callback function for empty slots in the array.

If you call map() on a sparse array (an array that has empty slots), it will preserve those empty slots in the new array. The callback function is simply not invoked for the empty slots, ensuring the length and structure of the original array are maintained perfectly.

javascript
const sparse = [1, , 3];
const mapped = sparse.map(x => x * 2);
console.log(mapped);
Output
[ 2, <1 empty item>, 6 ]

Misconceptions

Edge Cases

Check yourself

Is it true that map() modifies the original array?

Reveal answer

map() creates and returns a completely new array, leaving the original array untouched. — map() creates and returns a completely new array, leaving the original array untouched.

Is it true that You can stop or break out of a map() loop early?

Reveal answer

You cannot break or continue in a map() callback. It will always process every element (except empty slots). — You cannot break or continue in a map() callback. It will always process every element (except empty slots).

Is it true that If you don't return anything, the element is skipped?

Reveal answer

The element is not skipped. If you don't return, the new array gets 'undefined' at that position. — The element is not skipped. If you don't return, the new array gets 'undefined' at that position.

Challenges

Challenge 1 +100 XP

Use map() to convert an array of temperatures from Celsius to Fahrenheit. The formula is: (C * 9/5) + 32.

javascript
  • Test 1 — expects "[ 32, 50, 68 ]"
Show solution (0 XP)
const celsius = [0, 10, 20];
const fahrenheit = celsius.map(c => (c * 9/5) + 32);
console.log(fahrenheit);

🐞 Bug Hunt +100 XP

This code is supposed to create an array of formatted prices, but it's returning undefined. Fix the bug.

This code runs. It just does the wrong thing. Read it, find the defect, fix it — the tests below decide when you are right.

javascript
  • Test 1 — expects "[ '$10', '$20' ]"
Show solution (0 XP)
const prices = [10, 20];
const formatted = prices.map(price => {
  return '$' + price;
});
console.log(formatted);