Describes the use of the forEach () method in JavaScript, including javascriptforeach.
The forEach () method of the JavaScript array calls each element in the array.
Syntax
array.forEach(callback[, thisObject]);
The following is the detailed information about the parameters:
- Callback: each element of the function test array.
- ThisObject: the object used for executing the callback.
Return Value:
Returns the created array.
Compatibility:
This method is a JavaScript extension to the ECMA-262 standard; so it may not exist in other implementations of the standard. To make it work, you need to add the top of the following script code:
if (!Array.prototype.forEach){ Array.prototype.forEach = function(fun /*, thisp*/) { var len = this.length; if (typeof fun != "function") throw new TypeError(); var thisp = arguments[1]; for (var i = 0; i < len; i++) { if (i in this) fun.call(thisp, this[i], i, this); } };}
Example:
This produces the following results:
[0] is 12[1] is 5[2] is 8[3] is 130[4] is 44