map()

🎉
💨
Transform every item! Like sending trains through a magic tunnel!

The map() Method

The map() method creates a new array by transforming every item.

Each item goes through a function, and the results become a new array!

💡
💨

How map() Works

1const numbers = [1, 2, 3, 4, 5];
2
3// Double every number
4const doubled = numbers.map(n => n * 2);
5console.log(doubled); // [2, 4, 6, 8, 10]
6
7// Square every number
8const squared = numbers.map(n => n * n);
9console.log(squared); // [1, 4, 9, 16, 25]
10
11// Original is unchanged
12console.log(numbers); // [1, 2, 3, 4, 5]
🤔
💨

map() Creates a NEW Array

map() doesn't change the original array - it makes a brand new one with the transformed items!

Think of it like a transformation tunnel: the original train cars go in, and completely new, transformed cars come out the other side!

🎮

Try It Yourself!

Send your numbers through the transformation tunnel! Pick a transformation to apply to all items.

[
1
[0]
2
[1]
3
[2]
4
[3]
5
[4]
]
length: 5