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 ].

Discover other journeys with this module

What students will learn in this module

Every question in Module 25

Quick questions

  1. Does forEach() call a function once for each array element?ANoBYes
  2. Is 'forEach' spelled with a capital E in the middle?ANoBYes
  3. Does forEach() need a function passed to it?AYesBNo
  4. Is 'ForEach' with a capital F valid JavaScript syntax?ANoBYes
  5. 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 does
nums.forEach(function(n) { console.log(n); })Match it to what it does
Runs a function once for every element in an arrayMatch it to what it does
Holds the current element's value during each callMatch it to what it does
fruits.ForEach(function(fruit) { ... })Match it to what it does
fruits.forEach(fruit) { ... }Match it to what it does

Fill in the blanks

  1. fruits._______(function(fruit) {

    Hint: Type the array method that runs a function for each element.

  2. fruits.forEach(________(fruit) {

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

  3. colors._______(function(color) {

    Hint: Type the array method that runs a function for each element.

  4. colors.forEach(________(color) {

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

  5. 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);