forEach method in Arrays – Functional Programming in Javascript Part 3

tn500w_javascript-logo

forEach method in Arrays – Functional Programming in Javascript Part 3

In Javascript arrays are objects. It has a method forEach(fn) that takes function as argument and executes it for each element of the array. This function can have element of the array, index and array itself as argument.

var myArray=[10, 20, "Hello", {}];

var myFunction=function(item, index, array){
    console.log("For an element: "+item); 
}

myArray.forEach(myFunction);

Function expression myFunction will be executed for each element of the myArray array.

About Anoop Kumar Rai