Arrays – map(), online
Six activities your students do on their own devices. See who's finished, and their scores.
Learning
Understanding
Testing
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.
PREVIOUS · MODULE 25Arrays – forEach()
YOU ARE HERE · MODULE 26Arrays – map()
NEXT · MODULE 27Arrays – filter()
What students will learn in this module
Every question in Module 26
Quick questions
- Does map() create a new array?ANoBYes
- Does the original array change after calling map()?ANoBYes
- Is a return statement needed inside map()'s function?AYesBNo
- Does forEach() return a new array like map() does?ANoBYes
- 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 doesusers.map(function(u) { return u.name; })Match it to what it doesRuns a function on every element to build a new arrayMatch it to what it doesSends the transformed value back into the new arrayMatch it to what it doesnums.Map(function(n) { return n * 2; })Match it to what it doesnums.map(function(n) { n * 2; })Match it to what it doesFill in the blanks
let doubled = nums.___(function(n) {Hint: Type the array method that returns a new array.
let doubled = nums.map(________(n) {Hint: Type the keyword used to define the function passed to map.
______ n * 2;
Hint: Type the keyword that sends a value back from a function.
let names = users.___(function(u) {Hint: Type the array method that returns a new array.
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]
