JavaScript Array forEach() Method
You are Here:
JavaScript Array forEach() Method
The forEach()
method executes a provided function once for each array element.
Note: This method does not alter the original array.
Example
HTML Online Editor
<!DOCTYPE html>
<html>
<body>
<p>Multiply all element in an array by 10<p>
<script>
var myNumber = [1, 2, 3, 4, 5, 6, 7];
var crossNumber = 10;
function multiply(num) {
document.write(num +" × " +crossNumber +" = " +num*crossNumber +"<br>");
}
myNumber.forEach(multiply);
</script>
</body>
</html>
Syntax
Array.forEach(function(value, index, arr), thisArg);
//or
Array.forEach(functionName);
Parameter Values
Value | Type | Explanation |
---|---|---|
function(value, index, arr) | Required | Function to test for each element in an array. Value - Specifies a value or element in an array. index - Specifies the index of a value or element in an array. arr - Specifies the whole array. |
thisArg | Optional | Specifies a value against which each element in an array is tested. |
Example
HTML Online Editor
<!DOCTYPE html>
<html>
<body>
<p>Multiply all element in an array by 10<p>
<script>
var myNumber = [1, 2, 3, 4, 5, 6, 7];
function multiply(num, index, arr) {
document.write(num+" × "+this+" = "+num*this+"<br>");
}
myNumber.forEach(multiply, 10);
</script>
</body>
</html>
Reminder
Hi Developers, we almost covered 97% of JavaScript Tutorials with examples for quick and easy learning.
We are working to cover every Single Concept in JavaScript.
Please do google search for:
Join Our Channel
Join our telegram channel to get an instant update on depreciation and new features on HTML, CSS, JavaScript, jQuery, Node.js, PHP and Python.
This channel is primarily useful for Full Stack Web Developer.