JavaScript

Get the Sum of an Array of Numbers in JavaScript

An array is used in JavaScript to store different items of the same data type, such as an array of strings containing values ​​of string data types, an array of numbers containing numeric values or an integer data type, and so on. In some situations, programmers need to add all the elements of a number array. Therefore, the question is, how to calculate and get the sum of the number of array elements in JavaScript?

This tutorial will explain the methods for calculating the sum of the array’s elements in JavaScript.

How to Calculate the Sum of an Array of Numbers in JavaScript?

The sum of an array of numbers can be calculated in a variety of ways, including:

Let’s check out each of them one by one!

Method 1: Calculate the Sum of an Array of Numbers Utilizing for Loop

To calculate the sum of an array of numbers, the most basic and traditional method is the “for” loop. In this approach, iterate the array to the length and add them.

Syntax

Utilize the following syntax to use the for loop:

for (i = 0; i < lengthOfArray; i++){

// statement

}

Example

Create an array of numbers:

var array = [8, 14, 20, 23];

Create a variable “arrayElementSum” and set its initial value to 0:

var arrayElementSum = 0;

Add the elements of an array by iterating the array until the array’s length using for loop:

for (var i = 0; i < array.length; i++) {

arrayElementSum += array[i];

}

Print the sum of the array’s elements using the “console.log()” method:

console.log(arrayElementSum);

Output

The output shows the sum of all the elements in an array as “65”.

Calculate the Sum of an Array of Numbers Utilizing for-of Loop

Instead of a “for” loop, there are more best approaches in JavaScript, such as the “for-of” method.

Example

Let’s use the for-of loop for each iteration of the array to add the array elements:

for (const sum of array) {

  arrayElementSum += sum;

}

Output

The output indicates that with the help of the “for-of” loop, the elements of an array that are successfully added and displayed “65” as their sum.

Calculate the Sum of an Array of Numbers Utilizing forEach Loop

A “forEach” loop can be used as an alternative method to calculate the sum of an array of numbers. It is a predefined method in JavaScript.

Example

Use the same array created in the previous example and loop across the array using the array.forEach() loop method:

array.forEach(sum => {

  arrayElementSum += sum;

});

Output

Method 2: Calculate the Sum of an Array of Numbers Utilizing reduce() Method

The array of numbers can alternatively be summed using JavaScript’s array “reduce()” method. The “reduce()” method utilizes a reducer function to produce a single output value by calling it on each array element. It iterates through the numbers in an array, just like the for a loop.

Syntax

Follow the below-provided syntax for using the reduce() method:

reduce(function(preValue, currentValue, currentIndex, array) => {

// statement

}, initialValue)

In the above syntax:

  • The method takes two parameters, the callback function, or the initialValue.
  • The callback function further calls some parameters, including “preValue”, “currentValue”, “currentIndex”, and an “array”, where the last two parameters are optional.

Example

Use the below lines of code while using the “reduce()” method for adding the elements of the array:

var arrayElementSum = array.reduce(function(a, b){

     return a + b;

   }, 0);
  • To iterate across the array, use the reduce() method on Array.
  • Set the reduction method’s initial value to 0.
  • It returns the sum of the current value and the collected value at the end of each iteration.

Output

That’s all about getting the array of numbers’ sum in JavaScript.

Conclusion

For getting the sum of the elements of a number array, the “for” loop, “for-of” loop, “forEach” loop, or the “reduce()” method is used. Iterative loops are useful for calculating the sum of array elements because they seek to consider each element separately. All the mentioned approaches are discussed in this article with examples.

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.