×

javascript · level 2 · unit 6 · lesson 1

  1. meet it
  2. guess first
  3. your turn
  4. fix it
  5. nailed it

Tap a green word if you get lost.

meet it

builds a new array by changing every item.

.map() takes a function, runs it on every item, and collects each answer into a new array, in the same order. prices.map((price) => price * 2) doubles every price, and the old array stays as it was.

Notice that arrow function has no curly brackets and no return. When an arrow function is a single expression, it hands that value back by itself: an . Add curly brackets and you must write return yourself.

One item in, one item out: map always gives back an array of the same length.

const prices = [2, 5, 10];
const doubled = prices.map((price) => price * 2);
const labels = prices.map((price) => `£${price}.00`);
console.log(doubled);
console.log(labels);
console.log(prices);

(price) => price * 2 does the same as (price) => { return price * 2; }, only shorter.