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.
- 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:
- currentValue: Current element
- index: Current position
- 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:
2
3
Example 2: Multiply Numbers
let nums = [1,2,3];
nums.forEach(num => console.log(num*2));
Output:
4
6
Example 3: Print Strings
let fruits= ["Apple", "Mango"];
fruits.forEach( fruit => console.log(fruit));
Output:
Mango
Example 4: Display Index with value
let colors =["Red", "Blue", "Green"];
colors.forEach( (item,index)=> console.log(index,item));
Output:
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:
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:
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:
Real-Life Example 2: Todo List Display
let todos= ["Study", "Exercise", "Sleep"];
todos.forEach(task => console.log(task));
Output:
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:
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:
forEach method β Interview Questions
Q 1: What is the forEach() method?
Q 2: Does forEach() return a value?
Q 3: Can forEach() break the loop?
Q 4: Does forEach() modify the original array?
Q 5: Is forEach() asynchronous?
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.