How To Iterate an Array using forEach Loop in JavaScript? Last Updated : 17 Nov, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report Iterating through an array in JavaScript can be done in multiple ways. One of the most common methods is using the traditional for loop. The modern approach to iterate the array which is by using the forEach method. This method is different from the traditional approach as it uses a functional approach. It provides us with new ways to access each element of the array as well as the whole array. Since we use a functional approach therefore we can also use arrow function syntax with this method.Syntax:arr.forEach(function(val, index, wholeArr)){ . . .} JavaScript let arr = [2, 4, 7, 8, 1] arr.forEach(function(val){ console.log(val) }) Output2 4 7 8 1 Example 2: Iterating through an array using the arrow function. JavaScript let arr = [2, 4, 7, 8, 1] arr.forEach((val, index) => {console.log(`val=${val} index=${index} `)}) Outputval=2 index=0 val=4 index=1 val=7 index=2 val=8 index=3 val=1 index=4 Example 3: Accessing the whole array with the third optional parameter. JavaScript let arr = [2, 4, 7, 8] arr.forEach((val, index, wholeArr) => {console.log(wholeArr)}) Output[ 2, 4, 7, 8 ] [ 2, 4, 7, 8 ] [ 2, 4, 7, 8 ] [ 2, 4, 7, 8 ] Comment More infoAdvertise with us Next Article How to use forEach with an Array of Objects in JavaScript ? S shobhit_sharma Follow Improve Article Tags : JavaScript Web Technologies Similar Reads How to use forEach with an Array of Objects in JavaScript ? Using the forEach() method with an array of objects in JavaScript is essential for iterating over collections and performing operations on each object. This guide explores effective techniques to utilize forEach() for array manipulation, enhancing your coding skills. Syntax: array.forEach( function( 3 min read How to Push an Object into an Array using For Loop in JavaScript ? JavaScript allows us to push an object into an array using a for-loop. This process consists of iterating over the sequence of values or indices using the for-loop and using an array manipulation method like push(), to append new elements to the array. We have given an empty array, and we need to pu 3 min read How to use async/await with forEach loop in JavaScript ? Asynchronous is popular nowadays because it gives functionality of allowing multiple tasks to be executed at the same time (simultaneously) which helps to increase the productivity and efficiency of code. Async/await is used to write asynchronous code. In JavaScript, we use the looping technique to 2 min read How to Change Values in an Array when Doing foreach Loop in JavaScript ? The forEach() method in JavaScript is a popular way to iterate through an array. Unlike other looping methods, forEach() passes a callback function for each element in the array. While forEach() itself does not directly change the array, you can modify elements within the array by using the callback 3 min read How to Loop Through an Array in JavaScript? Here are the various ways to loop through an array in JavaScript1. Using for LoopThe for loop is one of the most used ways to iterate over an array. It gives you complete control over the loop, including access to the array index, which can be useful when you need to modify elements or perform other 4 min read How to Iterate Over Characters of a String in JavaScript ? There are several methods to iterate over characters of a string in JavaScript. 1. Using for LoopThe classic for loop is one of the most common ways to iterate over a string. Here, we loop through the string by indexing each character based on the string's length.Syntaxfor (statement 1 ; statement 2 2 min read Like