How to use Array forEach() Method In Javascript

The array.forEach() method calls the provided function (a callback function) once for each element of the array. The provided function is user-defined, it can perform any kind of operation on an array.

Syntax:  

array.forEach(function callback(value, index, array) {
} [ThisArgument]);

Parameters: This function accepts three parameters as mentioned above and described below:  

  • value: This is the current value being processed by the function.
  • index: The item index is the index of the current element which was being processed by the function.
  • array: The array on which the .forEach() method was called.

Example: This example uses the forEach() method on an array for iterating every element of an array and printing every element of an array in a new line.

Javascript




let emptytxt = "";
let  Arr = [23, 212, 9, 628, 22314];
 
function itrtFunction(value, index, array) {
    console.log(value);
}
 
Arr.forEach(itrtFunction);


Output

23
212
9
628
22314


JavaScript Array Iteration Methods

JavaScript Array iteration methods perform some operation on each element of an array. Array iteration means accessing each element of an array.

There are some examples of Array iteration methods are given below:

Similar Reads

Method 1: Using Array forEach() Method

The array.forEach() method calls the provided function (a callback function) once for each element of the array. The provided function is user-defined, it can perform any kind of operation on an array....

Method 2: Using Array some() Method

...

Method 3: Using Array map() Method

The array.some() method checks whether at least one of the elements of the array satisfies the condition checked by the argument function....