JavaScript

How to Compute the Sum and Average of Elements in an Array?

In various situations, developers need to compute the sum and the average of the elements of an array in JavaScript. For instance, it may be necessary to determine a student’s average score by adding up all of their exam results and dividing them by the total number of exams they have taken.

This post will illustrate the methods for computing the sum and the average of the array elements.

How to Compute/Calculate the Sum and Average of Elements in an Array?

To compute the sum and the average of the elements of an array, use the following approaches:

Method 1: Compute the Sum and Average of Elements in an Array Using “reduce()” Method

To compute the sum and average of the array elements, use the “reduce()” method. It uses the callback function that applies to every element of an array and gives a single reduced output. This method accepts two parameters a callback function and an optional initial value.

Syntax
Use the given-provided syntax for the reduce() method:

reduce((accumulator, currentValue) => {
// statement
}, initialValue)

Example
Create an array of the first 10 even numbers:

var array = [2, 4, 6, 8, 10, 12, 14, 16, 18, 20];

Call the “reduce()” method with array to get the sum of all the array elements and store it in a variable “sum”:

var sum = array.reduce((a, b) => a + b, 0);

Now, divide the sum with the length of an array to get the average of the array elements:

var average = sum / array.length;

Print the result “sum” and the “average” on the console:

console.log("Sum of Array Elements: " + sum);
console.log("Average of Array Elements: " + average);

The output displays the sum of first 10 even numbers that is “110” and the average is “11”:

Method 2: Compute the Sum and Average of Elements in an Array Using “for-of” Loop

Another way to calculate the sum and average of the array elements is the traditional loops, such as for loop, while loop, for-of loop and so on. Here, we will discuss the “for-of” loop for computing the sum and the average of the array elements.

Syntax
The following syntax is utilized for the for-of loop:

for (var x of array) {
 //statement
}

Example
Create a variable “sum” and assign a value 0 to it:

var sum = 0;

Use the for-of loop and add the sum of the elements in the variable “sum”:

for (var x of array) {
 sum += x;
}

For calculating an average of array elements, divide the sum of the elements with the length of the array:

var average = sum / array.length;

Finally, print the result on the console:

console.log("Sum of Array Elements: " + sum);
console.log("Average of Array Elements: " + average);

Output

That’s all about the computing of sum and the average of the array elements.

Conclusion

For calculating the sum and the average of the array, use the “reduce()” method or the loops, such as the “for” loop, “while” loop, or the “for-of” loop. However, the “reduce()” method is the most commonly used method for computing the sum and average of the array elements. This post will illustrate the methods for computing the sum and the average of the array elements.

About the author

Farah Batool

I completed my master's degree in computer science. I am an academic researcher and love to learn and write about new technologies. I am passionate about writing and sharing my experience with the world.