forEach method in Arrays – Functional Programming in Javascript Part 3

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.

Leave a Reply

Your email address will not be published. Required fields are marked *