## DEV Community

Jack Pritom Soren

Posted on

# Higher Order Function Examples

Example-1 without higher order function

``````var number = [1,2,3];
var result =[];

for (let i = 0; i < number.length;i++)
{
result.push(number[i]*2);
}

console.log(result);
``````

Example-1 with higher order function

``````var number = [1,2,3];

var result = number.map((item)=>{
return item * 2;
})

console.log(result);
``````

Example-2 without higher order function

``````var players =
[
{
name: 'A',
avg: 33.2
},
{
name: 'B',
avg: 37.3
},
{
name: 'C',
avg: 38.32
},
{
name: 'D',
avg: 40.76
}
];

var UpdateResult = [];

for(var i = 0; i <players.length; i++)
{
if(players[i].avg >= 37)
{
UpdateResult.push(players[i]);
}
}

console.log(UpdateResult);
``````

Example-2 with higher order function

``````var players =
[
{
name: 'A',
avg: 33.2
},
{
name: 'B',
avg: 37.3
},
{
name: 'C',
avg: 38.32
},
{
name: 'D',
avg: 40.76
}
];

var UpdateResult = players.filter((player) => player.avg >= 37);

console.log(UpdateResult);
``````

Example-3 without higher order function

``````var arr = [1, 2, 3, 4];

function getArraySum(array) {
var total = 0;
for (var i = 0; i < array.length; i++) {
total += array[i];
}
}

var result = getArraySum(arr);

console.log(result)

``````

Example-3 with higher order function

``````var arr = [1, 2, 3, 4];

var total = arr.reduce((sum, item) => {
return result = sum + item;
})

console.log(result);

``````

Mustafa

I really enjoyed it during the read time. Thanks for the content! It was really beneficial for me.

Jack Pritom Soren

You are welcome!

Florian Woelki

Would be awesome, if there is an example for `reduce`

Jack Pritom Soren