Arrays

forEach() in JavaScript

What is forEach()?

forEach() executes a provided function once for each array element, and is commonly used to loop through arrays without needing a traditional for loop.

Syntax

array.forEach(function(element, index) {
    // code
});

Example

let fruits = ["Apple", "Banana", "Mango"];
fruits.forEach((fruit, index) => {
    console.log(index + ": " + fruit);
});
// 0: Apple
// 1: Banana
// 2: Mango

Unlike map(), forEach() does not return a new array — it's used purely for performing an action on each element.

Ready to master real-world JavaScript development?

Learn JavaScript hands-on with mentor-led, live sessions.

Explore Course