Syntax
arr.map(item => transformedItem)Examples
Basic Transformation
Transforming each element of an array.
const numbers = [1, 2, 3, 4, 5];
const doubled = numbers.map(n => n * 2);
console.log(doubled); // [2, 4, 6, 8, 10]
const squared = numbers.map(n => n ** 2);
console.log(squared); // [1, 4, 9, 16, 25]Transforming Objects
Extracting or reshaping data from an array of objects.
const users = [
{ firstName: "Alice", lastName: "Smith" },
{ firstName: "Bob", lastName: "Jones" }
];
const fullNames = users.map(u => `${u.firstName} ${u.lastName}`);
console.log(fullNames); // ["Alice Smith", "Bob Jones"]
// Creating new shaped objects
const summaries = users.map(u => ({ name: u.firstName, initial: u.lastName[0] }));
console.log(summaries); // [{ name: "Alice", initial: "S" }, { name: "Bob", initial: "J" }]map() with Index
Using the optional index parameter passed to the callback.
const items = ["apple", "banana", "cherry"];
const numbered = items.map((item, index) => `${index + 1}. ${item}`);
console.log(numbered);
// ["1. apple", "2. banana", "3. cherry"]Best practices
- Use map() only when you need the resulting array - if you just need to run side effects for each item, use forEach() instead
- Remember map() always returns a new array of the same length, one output for every input - use filter() first if you need to remove elements too
- Keep the mapping function pure (no side effects) for predictable, easy-to-reason-about code
- Wrap object literal returns in parentheses when using arrow functions: item => ({ key: value })
At a glance
- Purpose
- Application logic and interaction
- File extension
- .js ยท .mjs
- Runs in
- Browsers and JavaScript runtimes
- Usually used with
- HTML, CSS and Web APIs
Specifications & further reading
Related JavaScript documentation
Array Basics
Arrays are ordered, zero-indexed collections that can hold values of any type, including a mix of types. They can be created with array literal syntax [] or the Array constructor. The length property reflects the number of elements, and static methods like Array.isArray(), Array.from(), and Array.of() help create or check arrays.push(), pop(), shift(), unshift()
These four methods add or remove elements from the ends of an array, all mutating the original array in place. push() and pop() work on the end of the array (fast). shift() and unshift() work on the beginning (slower, since remaining elements must be re-indexed).slice() and splice()
slice() returns a shallow copy of a portion of an array as a new array, without modifying the original - useful for extracting a section. splice() modifies the original array in place by removing, replacing, or inserting elements at a specific position, and returns the removed elements.concat() and join()
concat() merges two or more arrays into a new array, without modifying the originals. join() converts all array elements into a single string, separated by a specified delimiter (comma by default) - the reverse operation of String.prototype.split().
Arrays are ordered, zero-indexed collections that can hold values of any type, including a mix of types. They can be created with array literal syntax [] or the Array constructor. The length property reflects the number of elements, and static methods like Array.isArray(), Array.from(), and Array.of() help create or check arrays.push(), pop(), shift(), unshift()
These four methods add or remove elements from the ends of an array, all mutating the original array in place. push() and pop() work on the end of the array (fast). shift() and unshift() work on the beginning (slower, since remaining elements must be re-indexed).slice() and splice()
slice() returns a shallow copy of a portion of an array as a new array, without modifying the original - useful for extracting a section. splice() modifies the original array in place by removing, replacing, or inserting elements at a specific position, and returns the removed elements.concat() and join()
concat() merges two or more arrays into a new array, without modifying the originals. join() converts all array elements into a single string, separated by a specified delimiter (comma by default) - the reverse operation of String.prototype.split().