Categories

# How to calculate average with the array reduce method in JavaScript?

Sometimes, we want to calculate average with the array reduce method in JavaScript.

In this article, we’ll look at how to calculate average with the array reduce method in JavaScript.

### How to calculate average with the array reduce method in JavaScript?

To calculate average with the array reduce method in JavaScript, we can call `reduce` with a callback to return the partial sum of the average.

For instance, we write:

``````const array = [129, 139, 155, 176]
const average = array.reduce((avg, value, _, {
length
}) => {
return avg + (value / length);
}, 0);
console.log(average)
``````

to call `array.reduce` with a callback that return `avg + (value / length)`, which is the partial sum of the average so far.

The 2nd argument of 0 so the initial value of `avg` is 0.

Therefore, `average` is 149.75.

### Conclusion

To calculate average with the array reduce method in JavaScript, we can call `reduce` with a callback to return the partial sum of the average.

## By John Au-Yeung

Web developer specializing in React, Vue, and front end development.