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); |
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:
- Using Array forEach() Method
- Using Array some() Method
- Using Array map() Method