DEV Community

subash
subash

Posted on

JavaScript Array Iteration Methods Explained (forEach, map, filter, reduce)

forEach LOOP

This loop will repeat how long the array length.

In this case what was happened is forEach have the argument of 'display' which is function , this will call that display function , that display function have three parameter like display(val,d,s).

First forEach is one of the inbuilt function that return array val , index number and then entire array , already we gave three parameter(val,d,s). these first parameter is contain the array value , and second parameter is contain array index number then final parameter have the entire array,

forEach(display);
function forEach (arg){
    arg(10,0,1)
}
Enter fullscreen mode Exit fullscreen mode

here what was happened is display is the argument this will stored in arg parameter , and then in this function have arg(10,0,10) arg will consider as display so display(10,0,10) .
so in this page already display have array value, index number, and then entire array right , in this case we add one more aruguments which is display(10,0,10) so this arugument will add onces array length will complete.

map()

It is same like forEach but what the diff is we can return the value in function forEach not return like this
for example;
const mark = [2,3,3,3];
const output= mark.map (display);
function display(val,d,s){
return val+d
}

console.log(output)

this work in map , but not in forEach,

mapFlat()

References;

1.https://dev.to/sivakumar_mathiyalagan_/array-iteration-methods-ko4
2.https://www.w3schools.com/js/js_array_iteration.asp#mark_map

Top comments (0)