JS-063

Beginner6 min~6 min read#array#map#arrays
map()
Transform every element
Goal
Learn to create a new array by transforming every element with map().
Explanation
- map() iterates over every element in an array.
- A function runs for each element.
- The results are collected in a new array.
- The original array does not change.
- map() is often used to prepare data.
Analogy
Imagine a conveyor belt. Regular apples go in, and peeled apples come out the other side. Every element undergoes the same transformation.
Code
const numbers = [1, 2, 3];
const doubled = numbers.map(
n => n * 2
);
console.log(doubled);Result: [2, 4, 6]
Common mistake
Beginners often think map() changes the original array. In fact, it creates a new one.
Remember
- ✦ map() creates a new array.
- ✦ The original array does not change.
- ✦ Every element undergoes a transformation.
Quick Quiz
What does [1, 2, 3].map(n => n * 2) return?