Skip to main content
🟨 JavaScript Foundations

Reading map() πŸ—ΊοΈ

Next you'll transform whole lists at once with .map(). Let's read it first.

.map() builds a new array by running a little function on every item. The original array is left untouched.

[1, 2, 3].map((n) => n + 1); // [2, 3, 4]

That (n) => n + 1 is an arrow function β€” a quick way to say "take n, hand back n + 1." map runs it once per item and collects the results into a new array.

Read each snippet and predict the new array. πŸ‘‡

What does this produce?

[1, 2, 3].map((n) => n * 10);

What does this produce?

[5, 6].map((n) => n - 1);

After [1, 2, 3].map((n) => n + 1) runs, the ORIGINAL array is…