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.
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.
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.
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' ]
Your output
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 outputjavascript
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 ]
You predicted
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.
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.
Reality: map() creates and returns a completely new array, leaving the original array untouched.
Wrong Belief: You can stop or break out of a map() loop early.
Reality: You cannot break or continue in a map() callback. It will always process every element (except empty slots).
Wrong Belief: If you don't return anything, the element is skipped.
Reality: The element is not skipped. If you don’t return, the new array gets ‘undefined’ at that position.
Wrong Belief: map() works on objects.
Reality: map() is strictly an Array method. To iterate over an object’s properties, use Object.keys() or Object.entries() first.
Edge Cases
Calling map on an empty array returns an empty array immediately without invoking the callback.
Mapping a sparse array preserves the empty slots in the new array.
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.