In continuation of series of ECMAScript 5. This article describes forEach feature of Array in ECMAScript 5. forEach loops through each element of array and invokes callbackfn for each element. forEach doesn't have any return value. forEach method counts the length of array before invoking the function. If any new element added in the array in callbackfn forEach won't loop for those elements.
Array.forEach()
Signature
array.forEach(callbackfn [, thisArg])
callbackfn - It tests array elements. callbackfn should be defined as function (value[,index [,array]]).
thisArg- thisArg is optional. It is used to pass to callbackfn as this value.
Returns
N/A
Example
var arr = [87, 22, 54, 9, 39, 30, 31, 76, 39, 2];
arr.forEach(function(value, index, arr){ arr[index] = arr[index] + 10;});
This ends the forEach feature of Array in ECMAScript 5.
Note: some and every counts the length of array before invoking the function. If any new element added in the array in callbackfn forEach won't loop for those elements.
|