forEach Method in JavaScriptThe forEach method is a built-in method in JavaScript that allows you to execute a function for each item in an array. It’s useful when you want to perform an action on every item in the array without needing to manually manage a loop.
forEach Method?The forEach method is a way to iterate through all the elements in an array and apply a function to each element. Unlike traditional loops, forEach makes your code cleaner and easier to read.
The basic syntax of forEach is:
array.forEach(function(element, index, array) {
// Code to execute for each element
});
Here’s what each part means:
array: The array you want to loop through.element: The current item in the array being processed.index: The index of the current item in the array (optional).array: The original array (optional).forEach with an ArrayLet’s see an example of how to use forEach to iterate through an array and log each item to the console.
const fruits = ["apple", "banana", "cherry"];
fruits.forEach(function(fruit) {
console.log(fruit);
});
In this example:
fruits is an array of strings representing different fruits.forEach iterates over each item in the fruits array.fruit parameter in the function represents each item as the loop goes through the array.console.log(fruit) prints each fruit to the console.forEach with Index and Array ParametersYou can also use the index and the original array within the function. Here’s an example:
const fruits = ["apple", "banana", "cherry"];
fruits.forEach(function(fruit, index, array) {
console.log("Index:", index, "Fruit:", fruit);
console.log("Original array:", array);
});
In this example:
index represents the position of the current item in the array.array is the original array, allowing you to reference it if needed.forEachYou can also use arrow functions with forEach to make your code more concise. Here’s how:
const fruits = ["apple", "banana", "cherry"];
fruits.forEach((fruit) => {
console.log(fruit);
});
In this example, the arrow function
(fruit) => { console.log(fruit); }
forEach does not return a value. It performs the action and then stops. If you need a transformed array, consider using map instead.forEach is great for performing actions on each item but is not suitable for cases where you need to break out of the loop early. For such cases, for or for...of loops are more appropriate.forEachTry these exercises to practice using forEach:
forEach loop to print each number in an array of numbers doubled.forEach to create a new array where each item is the length of the corresponding string in an array of strings.
const numbers = [1, 2, 3, 4, 5];
numbers.forEach((number) => {
const doubled = number * 2;
console.log("Doubled number:", doubled);
});
const strings = ["hello", "world", "JavaScript"];
const lengths = [];
strings.forEach((string) => {
lengths.push(string.length);
});
console.log("Lengths of strings:", lengths);
The forEach method is a useful way to loop through each item in an array and perform an action. It simplifies the process of iterating through arrays and makes your code more readable.