Arrays – forEach(), online
Six activities your students do on their own devices. See who's finished, and their scores.
Arrays – forEach() in one minute
What students need to know
This is exactly what the forEach() method does. The parameter stores the value of each element as forEach runs.
forEach is camelCase, so the E in 'Each' is capitalized. forEach() calls the function you give it, once per element.
Mistakes students make
- 'forEach' must start lowercase - capital F causes an error.
- 'forEach' needs a capital E - 'foreach' is wrong casing.
- This line is missing a semicolon, but that's not required in JS.
- The block must close with }, not ].
What students will learn in this module
Every question in Module 25
Quick questions
- Does forEach() call a function once for each array element?ANoBYes
- Is 'forEach' spelled with a capital E in the middle?ANoBYes
- Does forEach() need a function passed to it?AYesBNo
- Is 'ForEach' with a capital F valid JavaScript syntax?ANoBYes
- Does forEach() return a new array?ANoBYes
Key terms
Students type the meaning of:
forEach()
Match the pairs
fruits.forEach(function(fruit) { ... })Match it to what it doesnums.forEach(function(n) { console.log(n); })Match it to what it doesRuns a function once for every element in an arrayMatch it to what it doesHolds the current element's value during each callMatch it to what it doesfruits.ForEach(function(fruit) { ... })Match it to what it doesfruits.forEach(fruit) { ... }Match it to what it doesFill in the blanks
fruits._______(function(fruit) {Hint: Type the array method that runs a function for each element.
fruits.forEach(________(fruit) {Hint: Type the keyword used to define the function passed to forEach.
colors._______(function(color) {Hint: Type the array method that runs a function for each element.
colors.forEach(________(color) {Hint: Type the keyword used to define the function passed to forEach.
nums._______(function(n) {Hint: Type the array method that runs a function for each element.
Spot the error
Question 1
Aa.ForEach(function(x) {Blet a = [1, 2];Cconsole.log(x);
Question 2
Alet b = [1, 2];Bb.foreach(function(x) {Cconsole.log(x);
Question 3
Alet c = [1, 2];Bc.forEach(function(x) {Cconsole.log(x)D});
Question 4
Alet d = [1, 2, 3];Bd.forEach(function(x) {Cconsole.log(x);D]
Question 5
Alet e = [1, 2];Bconsole.log(e);Clet f2 = 3;De.forEach((x) {Econsole.log(x);
