![]() |
VOOZH | about |
The JavaScript Array forEach() method is a built-in function that executes a provided function once for each array element. It does not return a new array and does not modify the original array. It's commonly used for iteration and performing actions on each array element.
array.forEach(callback(element, index, arr), thisArg);This method accepts five parameters as mentioned above and described below:
| Parameter | Description |
|---|---|
| callback | It is a callback function executes for each array element. |
| element | The current element being processed in the array. |
| index (Optional) | The index of current element. The array indexing starts from 0. |
| array (Optional) | The array on which forEach() is called. |
| thisArg (Optional) | Value to use as this when executing the callback function. |
This method may or may not change the original array provided as it depends upon the functionality of the argument function.
Example 1: Basic iteration to print array elements on console.
Example 2: Copy elements from one array to another using forEach().
Example 3: Calculates the square of every element of the array using forEach() method.