Below is the example of the Array forEach() method.
- Example:
<script>// JavaScript to illustrate forEach() methodfunctionfunc() {// Original arrayconst items = [12, 24, 36];const copy = [];items.forEach(function(item) {copy.push(item + item+2);});document.write(copy);}func();</script>chevron_rightfilter_none - Output:
26,50,74
The arr.forEach() method calls the provided function once for each element of the array. The provided function may perform any kind of operation on the elements of the given array.
Syntax:
array.forEach(callback(element, index, arr), thisValue)
Parameters: This method accepts five parameters as mentioned above and described below:
- callback: This parameter holds the function to be called for each element of the array.
- 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 Array.every 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 like 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.
Below example illustrate the Array forEach() method in JavaScript:
- Example: In this example the method forEach() calculates the square of every element of the array.
const items = [1, 29, 47]; const copy = []; items.forEach(function(item){ copy.push(item*item); }); print(copy);Output:
1,841,2209
Code for the above method is provided below:
Program:
<script> // JavaScript to illustrate forEach() method function func() { // Original array const items = [1, 29, 47]; const copy = []; items.forEach(function (item) { copy.push(item * item); }); document.write(copy); } func(); </script> |
Output:
1,841,2209
Supported Browsers: The browsers supported by JavaScript Array forEach() method are listed below:
- Google Chrome
- Microsoft Edge 9.0
- Mozilla Firefox 1.5
- Safari
- Opera
Recommended Posts:
- JavaScript Map forEach() Method
- ES6 | Array forEach() Method
- TypeScript | Array forEach() Method
- Iterate associative array using foreach loop in PHP
- How to remove an array element in a foreach loop?
- HTML DOM NodeList.forEach() Method
- Lodash _.forEach() Method
- Determine the first and last iteration in a foreach loop in PHP?
- Performance of for vs foreach in PHP
- AngularJS | angular.forEach() Function
- Node.js | forEach() function
- PHP | foreach Loop
- How to find the index of foreach loop in PHP ?
- How to compare two JavaScript array objects using jQuery/JavaScript ?
- How to move an array element from one array position to another in JavaScript?
- How to get the elements of one array which are not present in another array using JavaScript?
- What’s the difference between “Array()” and “[]” while declaring a JavaScript array?
- How to check whether an array is subset of another array using JavaScript ?
- How to convert Integer array to String array using JavaScript ?
- Difference between array.size() and array.length in JavaScript
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


