• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / JavaScript / Using JavaScript to Calculate Average of Array of Numbers

Using JavaScript to Calculate Average of Array of Numbers

September 19, 2022 Leave a Comment

To get the average of an array in JavaScript, we can use the JavaScript Array reduce() method along with the length property. Here is the setup for how this can be done:

var numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9];

function sumArray(total, item) {
  return total + item;
}

var averageOfNumbers = numbers.reduce(sumArray)/numbers.length;

console.log(averageOfNumbers);

#Output
5

The first part of the code above is just finding the sum of the array.

We then find the length of the array and divide the sum of the array by its length to get the average of the array.

We can also use a for loop to help get the sum of an array.

var numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9];

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

var averageOfArray = total/numbers.length;

console.log(averageOfArray);

#Output
5

When working with collections of data in JavaScript, the ability to summarize the data easily is valuable.

One such case is if you want to get the average of an array of numbers.

To calculate the average of an array of numbers in JavaScript, the easiest way is with the reduce() method along with the length property.

Below again we show you how to get the average of an array of numbers in JavaScript.

var numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9];

function sumArray(total, item) {
  return total + item;
}

var averageOfNumbers = numbers.reduce(sumArray)/numbers.length;

console.log(averageOfNumbers);

We can put our code in a function and shorten it so that we have a function that will return the average of any array passed to it.

function averageOfArray(arr){
  return arr.reduce((a, b) => a + b) / arr.length;
};

var numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9];

console.log(averageOfArray(numbers));

#Output
5

Using a For Loop to Get the Average of an Array in JavaScript

Another way you can get the average of an array of numbers is with a for loop.

First, we need to initialize a variable that will keep the running sum and then add each element to the running sum.

The we simply divide the sum of the array by the length of the array.

Below shows you how to get the average of an array of numbers in JavaScript with a for loop.

var numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9];

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

var averageOfArray = total/numbers.length;

We can put our code in a function to reuse the function to get the average of an array.

function averageOfArray(arr){
  var total = 0;
  for( var i =0; i<numbers.length; i++ ){
    total += numbers[i];
  }
  return total/arr.length;
};

var numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9];

console.log(averageOfArray(numbers));

#Output
5

Hopefully this article has been useful for you to learn how to use JavaScript to get the average of an array.

Other Articles You'll Also Like:

  • 1.  Remove Braces from String Using JavaScript
  • 2.  Get a List of Prime Numbers Using JavaScript
  • 3.  Using JavaScript to Get the Base URL
  • 4.  JavaScript onfocusout – How to Use the onfocusout Event on an HTML Form
  • 5.  Using JavaScript to Round to 4 Decimal Places
  • 6.  Adding CSS Important Property Using JavaScript
  • 7.  Finding the Length of a String in JavaScript
  • 8.  Using JavaScript to Remove Duplicates From Array
  • 9.  Using JavaScript to Remove Trailing Zeros From a Number or String
  • 10.  Remove the First Element From Array in JavaScript

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy