JavaScript Array forEach() Method
Last Updated :
09 Jul, 2024
The forEach() method calls a function for each element in an array. 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.
Syntax:
array.forEach(callback(element, index, arr), thisValue);
Parameters:
This method accepts five parameters as mentioned above and described below:
| Parameter | Description |
|---|
| callback | This parameter holds the function to be called for each array element. |
| element | The parameter holds the value of the elements being processed currently. |
| index | This parameter is optional, it holds the index of the current value element in the array starting from 0. |
| array | This parameter is optional, it holds the complete array on which forEach is called. |
| thisArg | This parameter is optional, it holds the context to be passed as this to be used while executing the callback function. If the context is passed, it will be used as this for each invocation of the callback function, otherwise undefined is used as default. |
Return value:
The return value of this method is always undefined. This method may or may not change the original array provided as it depends upon the functionality of the argument function.
Example 1: In this example, the Array.forEach() method is used to copy every element from one array to another.
JavaScript
// JavaScript to illustrate forEach() method
function func() {
// Original array
const items = [12, 24, 36];
const copy = [];
items.forEach(function (item) {
copy.push(item + item + 2);
});
console.log(copy);
}
func();
Example 2: In this example, the method forEach() calculates the square of every element of the array.
JavaScript
// JavaScript to illustrate forEach() method
function func() {
// Original array
const items = [1, 29, 47];
const copy = [];
items.forEach(function (item) {
copy.push(item * item);
});
console.log(copy);
}
func();
Supported Browsers:
JavaScript Array forEach() method- FAQs
What is the Array.forEach() method in JavaScript?
The Array.forEach() method executes a provided function once for each array element.
How does the Array.forEach() method work?
The Array.forEach() method takes a callback function as an argument, which is applied to each element of the array. The callback function receives three arguments:
- currentValue: The current element being processed in the array.
- index (optional): The index of the current element being processed.
- array (optional): The array forEach() was called upon.
Does Array.forEach() return a new array?
No, Array.forEach() does not return a new array. It always returns undefined.
Does Array.forEach() mutate the original array?
No, Array.forEach() does not mutate the original array. However, if the callback function modifies the array elements, those changes will affect the original array.
What are common use cases for forEach()?
Common use cases include iterating over an array to perform side effects, such as:
- Logging values to the console.
- Modifying elements of the array.
- Making API calls or other asynchronous operations for each element.
- Performing operations that do not require the creation of a new array, such as updating a UI or accumulating results in a variable outside the array.
Please Login to comment...