← JavaScript

Arrays – forEach()

Calls a given function once for every element in an array.

Understanding Arrays – forEach() in JavaScript

What it does: 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. JavaScript is case-sensitive, so the method must start lowercase. forEach() just runs the function - it doesn't build a new array. JavaScript's method is forEach(), not ForEach(). The parameter must be wrapped in a function definition.

Common mistakes: 'forEach' must start lowercase - capital F causes an error. 'forEach' needs a capital E - 'foreach' is wrong casing.

Class: _____________________  ·  Name: _____________________  ·  Date: _________

Free printable worksheet with full answer key — or add this module to a class and your students practise it interactively.

This module's key terms are also in a printable crossword (Modules 11–25) and crossword (Modules 21–30) and word search (Modules 1–29) and word search (Modules 21–52).

Take this worksheet further — free with Cloodit

Each concept is reinforced from every angle — learned, then applied, then tested.

Learning
Duo
Does forEach() call a function once for each array element?
YesNo
Matches
fruits.ForEach(function(fruit) { ... })
↓
Wrong - 'forEach' must start with a lowercase f
Applying
Meanings
Term
forEach()
Meaning
Calls a given function once for every element in an array.
Practice
let fruits = ["apple", "banana"];
Testing
Gaps
fruits.(function(fruit) {
Check Answer
Spots
1a.ForEach(function(x) {
2let a = [1, 2];

Cloodit turns curriculum-aligned coding content into journeys students work through one biome at a time.

The 'Woolly Mammoth' badge — Tundra Master, one of Cloodit's 106 collectible badges
Students earn collectable badges

Over 100 of them, across seven natural biomes.

Study Buddy
Students build a real study resource

Turns their work into revision material ready for Flash Cards and Exam study.

Diamond Dash — one of Cloodit's live, competitive Sprint games
Students lock in with Sprints

Live, competitive practice for the start of the lesson or a teacher-led lesson finisher.

A Cloodit certificate of achievement, ready to personalise and print
Custom printable certificates

Honour your top performers, or encourage a student who's trying hard — ready to print in seconds.

Quick Questions

1. Does forEach() call a function once for each array element?
A. No
B. Yes ✓
forEach() runs the given function for every element in the array.
2. Is 'forEach' spelled with a capital E in the middle?
A. No
B. Yes ✓
forEach is camelCase, so the E in 'Each' is capitalized.
3. Does forEach() need a function passed to it?
A. Yes ✓
B. No
forEach() calls the function you give it, once per element.
4. Is 'ForEach' with a capital F valid JavaScript syntax?
A. No ✓
B. Yes
JavaScript is case-sensitive, so the method must start lowercase.
5. Does forEach() return a new array?
A. No ✓
B. Yes
forEach() just runs the function - it doesn't build a new array.

Match the Pairs

fruits.forEach(function(fruit) { ... }) → Calls the function once for each element in fruits
nums.forEach(function(n) { console.log(n); }) → Prints every number in nums, one at a time
Runs a function once for every element in an array → array.forEach(function(item) { ... })
Holds the current element's value during each call → fruit
fruits.ForEach(function(fruit) { ... }) → Wrong - 'forEach' must start with a lowercase f
fruits.forEach(fruit) { ... } → Wrong - missing the 'function' keyword before the parameter

Key Term

forEach()
Calls a given function once for every element in an array.

Fill in the Blanks

1. fruits.forEach(function(fruit) {
Hint: Type the array method that runs a function for each element.
2. fruits.forEach(function(fruit) {
Hint: Type the keyword used to define the function passed to forEach.
3. colors.forEach(function(color) {
Hint: Type the array method that runs a function for each element.
4. colors.forEach(function(color) {
Hint: Type the keyword used to define the function passed to forEach.
5. nums.forEach(function(n) {
Hint: Type the array method that runs a function for each element.

Spot the Error

Question 1
A a.ForEach(function(x) {
B let a = [1, 2];
C console.log(x);
Line A — 'forEach' must start lowercase - capital F causes an error.
Question 2
A let b = [1, 2];
B b.foreach(function(x) {
C console.log(x);
Line B — 'forEach' needs a capital E - 'foreach' is wrong casing.
Question 3
A let c = [1, 2];
B c.forEach(function(x) {
C console.log(x)
D });
Line C — This line is missing a semicolon, but that's not required in JS.
Question 4
A let d = [1, 2, 3];
B d.forEach(function(x) {
C console.log(x);
D ]
Line D — The block must close with }, not ].
Question 5
A let e = [1, 2];
B console.log(e);
C let f2 = 3;
D e.forEach((x) {
E console.log(x);
Line D — Missing the 'function' keyword before the parameter.

Student with a class code? Join your class →