Arrays – map(), online

Six activities your students do on their own devices. See who's finished, and their scores.

Arrays – map() in one minute

What students need to know

This is exactly what the map() method does. The return keyword provides the value for each new array slot.

map() always returns a brand new array. map() leaves the original array untouched.

Mistakes students make

  • 'map' must start lowercase - a capital M causes an error.
  • Missing the 'return' keyword - without it, nothing is sent back.
  • forEach() doesn't return a new array - use map() instead.
  • This line is missing a semicolon, but that's not required in JS.

Discover other journeys with this module

What students will learn in this module

Every question in Module 26

Quick questions

  1. Does map() create a new array?ANoBYes
  2. Does the original array change after calling map()?ANoBYes
  3. Is a return statement needed inside map()'s function?AYesBNo
  4. Does forEach() return a new array like map() does?ANoBYes
  5. Is 'Map' with a capital M valid JavaScript syntax?AYesBNo

Key terms

Students type the meaning of:

  • map()

Match the pairs

nums.map(function(n) { return n * 2; })Match it to what it does
users.map(function(u) { return u.name; })Match it to what it does
Runs a function on every element to build a new arrayMatch it to what it does
Sends the transformed value back into the new arrayMatch it to what it does
nums.Map(function(n) { return n * 2; })Match it to what it does
nums.map(function(n) { n * 2; })Match it to what it does

Fill in the blanks

  1. let doubled = nums.___(function(n) {

    Hint: Type the array method that returns a new array.

  2. let doubled = nums.map(________(n) {

    Hint: Type the keyword used to define the function passed to map.

  3. ______ n * 2;

    Hint: Type the keyword that sends a value back from a function.

  4. let names = users.___(function(u) {

    Hint: Type the array method that returns a new array.

  5. let squares = nums.map(function(n) {
    ______ n * n;
    });

    Hint: Type the keyword that sends a value back from a function.

Spot the error

Question 1

Alet a = [1, 2];Blet b = a.Map(function(n) {Creturn n * 2;

Question 2

Alet c = [1, 2];Blet d = c.map(function(n) {Cn * 2;

Question 3

Alet e = [1, 2];Blet f = e.forEach(function(n) {Creturn n * 2;

Question 4

Alet g = [1, 2, 3];Blet h = g.map(function(n) {Creturn n * 2D});

Question 5

Alet i = [1, 2];Blet j = i.map(function(n) {Creturn n * 2;D]