How to Find Average in JavaScript

  1. Average Mathematical Formula
  2. JavaScript Average Method Example in HTML
  3. Alternative Way to Get the Same Results
How to Find Average in JavaScript

There is not any built-in method to get an average in JavaScript. We usually do different tricks and ways to achieve that functionality to count the average of defined values.

This article will discuss multiple examples to find an average using JavaScript by custom declared functions.

Average Mathematical Formula

To understand the JavaScript average, we must know the common mathematical formula to find an average.

average = (sum of all given values) / number of values

This is the formula where we need to perform two steps to get an accurate result.

  • calculate the sum of all given values
  • divide the result of the calculated sum with the number of values

JavaScript Average Method Example in HTML

Below is the HTML source code that will show a Click to get Average button; we will call a custom declared function on the click event of that button. The function will calculate an average of given array values using loop statements and finally returns the result.

We will display the calculated result in JavaScript alert() box.

<!DOCTYPE html>
<html>
<head>
    <title>
        HTML | JavaScript Average method example
    </title>
    <script type="text/javascript">
    </script>
 
</head>
<body>
 
  <h2>Hi Users Check Average of values (28, 78, 32, 14).</h2>
  <button onclick="calculateAverage()">Click to get Average</button>

<script>
function calculateAverage() {
    // you can change the values of array
   let array = [28, 78, 32, 14]
   let i = 0
   let sum = 0
   let len = array.length;
   let result = 0
    
    // loop for calculate sum of array values
    while (i < len) {
        sum = sum + array[i++];
    }
      result = sum / len
    // simply shows the result in alert box
    alert("Average of ("+array+") is :  "+result);
}
</script>
 
</body>
<html>

In this HTML page source, we have created a button called calculateAverage() on click event.

You can see the button Click to get Average that triggers the calculateAverage() method. In the body of that method, we have initialized an array of integer values and other useful variables to calculate an average.

Firstly, we must calculate the sum of all given values we are looking for to find an average. We iterate the code statement to traverse an array to all indexes to perform that small task.

To iterate the code statements, we usually implement loops based on conditions. We have used the while loop here until the given condition is true, which is current index is smaller than the length of an array or index < length.

We have calculated the sum of all array elements using our while loop logic and stored the result in the sum variable. We divided the sum variable value with the length of an array to calculate an average, and then we displayed the result in the alert() box.

Alternative Way to Get the Same Results

As shown below, you can get the same functionality by passing N numbers of values as a parameter to return an average.

function average(...nums) {
  let average = 0;
  for (const num of nums) {
    average += num / nums.length;
  }
  return average;
}
alert(average(28, 78, 32, 14));

We just used the average(...nums) method to receive the N number of values. We have used the for loop logic to count an average here.

Related Article - JavaScript Math