JavaScript forEach() Method

Introduction

The forEach() method in JavaScript is one of the most commonly used array methods for iterating through array elements. It executes a function once for every element in an array and is widely used to display data, perform operations, update values, and handle arrays efficiently.

What is JavaScript forEach() Method?

The JavaScript forEach() method executes a function once for every element in an array.

πŸ“–
Important:
  • It does Not Return a Value.
  • Iterates Over Every Element.
  • You cannot break out of a forEach loop using break or continue.
  • You can modify the original array within the forEach loop.

Syntax of forEach()

Basic syntax:


array.forEach( function(currentValue, index, array){ });

Arrow function syntax:


array.forEach( (item)=>{})

Explanation of Parameters:

  1. currentValue: Current element
  2. index: Current position
  3. array: Original array

Basic Examples of forEach()

You will see some basic examples of the forEach() method.

Example 1: Print Numbers


let nums = [1,2,3];
nums.forEach(num => console.log(num));

Output:

1
2
3

Example 2: Multiply Numbers


let nums = [1,2,3];
nums.forEach(num => console.log(num*2));

Output:

2
4
6

Example 3: Print Strings


let fruits= ["Apple", "Mango"];
fruits.forEach( fruit => console.log(fruit));

Output:

Apple
Mango

Example 4: Display Index with value


let colors =["Red", "Blue", "Green"];
colors.forEach( (item,index)=> console.log(index,item));

Output:

0 Red
1 Blue
2 Green

forEach() with Objects

In this example, you will see how forEach() works with Objects.


let users=[
{name:"John"},
{name:"Tom"}
];

users.forEach(user => console.log(user.name));

Output:

John
Tom

forEach() Using Arrow Functions

This is a modern syntax used with Arrow function.


let nums = [10,20];
nums.forEach(num => console.log(num));

Output:

10
20

Real-Life Example:

You will see some real-life examples of the forEach() method.

Real-Life Example 1: Shopping Cart Total


let prices= [100,200,300];
let total=0;

prices.forEach( price => total+=price);

console.log(total);

Output:

600

Real-Life Example 2: Todo List Display


let todos= ["Study", "Exercise", "Sleep"];

todos.forEach(task => console.log(task));

Output:

Study
Exercise
Sleep

Difference Between forEach() and map()

Many Beginners often confuse them.

Feature forEach() map()
Return Value undefined New array
Modify Data No Yes (returns transformed array)
Chain Methods No Yes
Example
nums.forEach(
  num => num * 2
);

// Output:
undefined
let nums = [1,2];

let result =
nums.map(
  num => num * 2
);

console.log(result);

// Output:
[2,4]

Common Mistakes in forEach()

Many Beginners make mistakes when using forEach().

Mistake 1: Expecting Return Value

Wrong:


let nums = [10,20];
let result= nums.forEach(num => num*2);

console.log(result);

Output:

undefined

Mistake 2: Using break inside the forEach() method

Wrong:


let nums = [10,20];
let result= nums.forEach(num => {
num*2
break;
}
);

console.log(result);

Output:

ERROR!

forEach method – Interview Questions

Q 1: What is the forEach() method?
Ans: forEach() executes a callback function once for each array element.
Q 2: Does forEach() return a value?
Ans: No, it always returns undefined.
Q 3: Can forEach() break the loop?
Ans: No, break and return cannot stop it.
Q 4: Does forEach() modify the original array?
Ans: It does not create a new array, but elements can be modified.
Q 5: Is forEach() asynchronous?
Ans: No, it runs synchronously.

forEach method– Objective Questions (MCQs)

Q1. forEach() is used to ______.






Q2. Does forEach() return a value?






Q3. forEach() can access which parameters?






Q4. Can break be used inside forEach()?






Q5. forEach() executes callback for ______ elements.






Conclusion

The JavaScript forEach() method is a simple and powerful way to iterate through arrays. It improves readability and is commonly used for displaying data, calculating totals, and processing arrays.

Unlike map(), forEach() does not return a new array, making it suitable for operations where only iteration is needed.