Development

How to Find The Average of Numbers in JavaScript: Various Methods

In this tutorial, we will explore the various methods and techniques that you can use to determine the average of input numbers using JavaScript.
Captain Salem 2 min read
How to Find The Average of Numbers in JavaScript: Various Methods

Calculating the average of numerical values is one of the most basic mathematical operations. Unlike other programming languages such as Python, JavaScript does not offer a native method for determining the average of numerical values.

However, we can take advantage of the provided JavaScript features to perform the average of values using some basic math and arrays.

Method 1 - Using a Loop

As you probably have guessed by now, a classic for loop is an excellent tool that we can use to calculate the sum of elements in an array and then divide by the length of the array.

This will allow us to get the average of the input values as demonstrated in the example below:

let numbers = [2, 4, 6, 8, 10];
let sum = 0;

for(let i = 0; i < numbers.length; i++) {
    sum += numbers[i];
}

let average = sum / numbers.length;
console.log(average);

This should return the average of the input numbers as:

6

Method 2 - Using Array.prototype.reduce()

In more modern versions of JavaScript, we can take advantage of the reduce method which is part of the JavaScript Array prototype to obscure the role of a for loop. We can demonstrate this as shown in the example below:

let numbers = [2, 4, 6, 8, 10];
let sum = numbers.reduce(function (accumulator, current) {
    return accumulator + current;
}, 0);

let average = sum / numbers.length;
console.log(average);

Output:

6

We can even make it shorter by using an arrow function as shown:

let numbers = [2, 4, 6, 8, 10];
let sum = numbers.reduce((accumulator, current) => accumulator + current, 0);

let average = sum / numbers.length;
console.log(average);

Output:

6

Method 3 - Creating a Function for Reusability

If you find yourself calculating averages frequently, you can create a function that calculates the average of an array. This allows you to reuse your code:

function calculateAverage(array) {
    let sum = array.reduce((accumulator, current) => accumulator + current, 0);
    return sum / array.length;
}

let numbers = [2, 4, 6, 8, 10];
let average = calculateAverage(numbers);
console.log(average); // Outputs: 6

Now you can use this function whenever you need to calculate an average.

Method 3 - Using Array.prototype.map()

Although the reduce() function does provide a more straightforward method of calculation, we can also use the map() function to accomplish a similar thing.

Consider the example shown below:

This method might be a bit overcomplicated for this task, but here it is for the sake of completion:

let numbers = [2, 4, 6, 8, 10];

let sum = 0;
numbers.map(num => sum += num);

let average = sum / numbers.length;
console.log(average);

Output:

6

Although the map method works, it tends to make a simple task seem a little overcomplicated. But that's what makes us feel like real developers ain't it :D

Side Note

All these methods have a time complexity of O(n) as you have to loop through each element in the array once.

Recap

In this tutorial, we learned how we can take advantage of various JavaScript features such as for loop, reduce function and the map function to calculate the average of numerical values. We hope this tutorial was helpful to you.

Share
Comments
More from GeekBits

Join us at GeekBits

Join our members and get a currated list of awesome articles each month.

Great! You’ve successfully signed up.

Welcome back! You've successfully signed in.

You've successfully subscribed to GeekBits.

Success! Check your email for magic link to sign-in.

Success! Your billing info has been updated.

Your billing was not updated.