How to Find the Average of an Array using JavaScript

Finding the average of all the numbers in an array using JavaScript is quite easy. We will look at how we can do it using plain JavaScript and also how to to find the average using jQuery. The syntax for looping over an array is a little different so it is worth taking a look at both.

The first thing we have to do to find the average of an array is to loop over all the elements. As we loop over the items we are going to keep a running total as well as a counter. At the end we will simply return the total divided by the number of items we encountered.

jQuery Array Average

// Just calculating it in the file
var someArray = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15];
var total = 0;
var count = 0;

jQuery.each(someArray , function(index, value){
    total += value;
    count++;
});

console.log(total / count);

We can also put this functionality inside a function to make it more reusable.

// Using a method
var someArray = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15];

function calculateAverageOfArray(array){
    var total = 0;
    var count = 0;

    jQuery.each(someArray , function(index, value)
    {
        total += value;
        count++;
    });

    return total/count;
}

console.log(calculateAverageOfArray(someArray));

We can also go one step further and wrap everything in a class. We can create a Calculator class which will have the functionality of calculating the average. We could also implement methods that would do other calculations, such as median, min, and max.

// Using a Class
class Calculator{
    constructor(){}

    static average(array){
        var total = 0;
        var count = 0;

        jQuery.each(array, function(index, value){
            total += value;
            count++;
        });

        return total / count;
    }
}

var someArray = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15];
console.log(Calculator.average(someArray));

If we would not have made the method in the Calculator class static we would have had to create an instance of the Calculator class in order to use it. You can copy and paste all the examples above into the console and they should return 8.

JavaScript Array Average

We can use plain JavaScript to calculate the average as well. There are actually a couple of different ways we can do it. The first way is to use a for loop to iterate over the array, and like the other examples we will simply add up the total and divide by the number of items.

// Using a for loop
someArray = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15];
var total = 0;
var count = 0;

for(var i = 0; i < someArray.length; i++){
    total += someArray[i];
    count++;
}

console.log(total / count);

The second way we can use regular JavaScript to calculate the average of an array is to use a forEach loop. It looks very much like the loop that we used with jQuery. We will go ahead and put this next example in a method.

// Using a for loop
someArray = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15];

function calculateAverage(array){
    var total = 0;
    var count = 0;

    array.forEach(function(item, index){
    total += item;
    count++;
    });

    return total / count;
}

console.log(calculateAverage(someArray));

We have now looked at how we can use JavaScript to find the average of an array, including using jQuery. You can do it straight in the file, in a method, or in a class if you wish.

If you want more JS tutorials then you can start with having a look at how to remove a comma from a string with JS.