Chapter VIII · Arrays

map

The map method transforms every item in an array by passing it through a callback function, returning a brand new array.

01 How It Works

Think of a factory processing line converting raw input materials into finished goods.

map iterates through each element, applies the transformation callback, and collects results into a new array.

example.js
const nums = [1, 2, 3];
const doubled = nums.map(n => n * 2); // [2, 4, 6]
  1. 1. Call .map() on an input array.

  2. 2. Provide a transformation callback function.

  3. 3. Receive the newly mapped array output.

02 Practical Example

Here is how you might see this concept applied in real-world code:

practical.js
const names = ["alice", "bob"];
const caps = names.map(n => n.toUpperCase());

Key Takeaway

Use map when you need to transform each element of an array into a new value.

Related Concepts