JS forEach


JS Array.forEach() method applies a function to each element of an array. It do not return value. The array may be modified.

var arr = new Array(1,2,3,4,5);
var sum = 0;
var ret = arr.forEach(function(x) {sum += x;});
alert(sum);  //15
alert(ret); //undefined

The function can take 3 arguments in the order of value, index (optional) and the array (optional).

var arr = new Array(1,2,3,4,5);
arr.forEach(function(val,indx,arr){arr[indx] *= 2;})
alert(arr);  //2,4,6,8,10