How To Find The Sum Of An Array Of Numbers in JavaScript

Do you want to learn how to find the sum of an array of numbers in Javascript? This article will discuss all the code examples that can quickly sum an array of numbers.

Let us assume that you have an array of numbers of sum limited length and in our example, I take the list of the first 10 natural numbers and use the below code example to get the sum.

Add an Array of Numbers Using Reduce in JavaScript

In JavaScript, there is a method called Reduce[1], The reduce() method runs a user-supplied “reducer” callback function on each element of the array, sending in the result of the previous element’s calculation. The reducer returns a single value for all entries of the array.

As in our case, the reduce() method will return the sum of an input array that I am going to provide. Let us see in the below code example the usage of reduce() in JavaScript.

//Initializing the Array with Some Items
let givenArray = [1,2,3,4,5,6,7,8,9,10]

//Initializing variable to store the sum
var totalSum = 0

//Creating a Reduce method to get the sum
//Here we are using Lamba to get two number from array
//And sum it.
totalSum = givenArray.reduce((firstNumber, secondNumber) => firstNumber + secondNumber, 0)

console.log("The total sum is " + totalSum)

Output:

The total sum is 55

As you can see in the above code, I used a lambda function to return the sum of two numbers from the array, and if you do not get two numbers then by default return 0. Now if you are not familiar with the lambda function then you can use the below code as a reference.

//Initializing the Array with Some Items
let givenArray = [1,2,3,4,5,6,7,8,9,10]

//Initializing variable to store the sum
var totalSum = 0

//Creating a Reduce method to get the sum
//Here we are using Function to get two number from array
//And sum it.
totalSum = givenArray.reduce(function (firstNumber, secondNumber){ 
    return firstNumber + secondNumber 
}, 0)

console.log("The total sum is " + totalSum)

Output:

The total sum is 55

Using For Loop To Get Sum Of An Array Of Numbers

This is the most simplified solution you can use. You can loop through the numbers one by one and keep adding to the sum variable that you want to print out.

Let us see in the below example code the usage of For Loop in JavaScript to add an array of numbers.

//Initializing the Array with Some Items
let givenArray = [1,2,3,4,5,6,7,8,9,10]

//Initializing variable to store the sum
var totalSum = 0

//using For Loop to add the sum
for(var index in givenArray){
    totalSum += givenArray[index]
}

console.log("The total sum is " + totalSum)

Output:

The total sum is 55

As you can see in the above code, I have successfully added the numbers using the For loop. And in my testing of the above two code examples, even though the lambda function uses fewer lines of codes but for loop much faster than the lambda function.

Using ForEach Loop To Get Sum Of An Array Of Numbers

Similar to For Loop you can use the ForEach loop to get the sum of an array of numbers. Since you are not required to get the index of the array for the ForEach loop. let us see in the below example code the usage of ForEach Loop in JavaScript.

//Initializing the Array with Some Items
let givenArray = [1,2,3,4,5,6,7,8,9,10]

//Initializing variable to store the sum
var totalSum = 0

//using ForEach Loop to add the sum
givenArray.forEach(number => {
    totalSum += number
    return totalSum;
});

console.log("The total sum is " + totalSum)

Output:

The total sum is 55

As you can see in the above code, I was able to add the number using ForEach Loop in JavaScript. This method also works at the same efficiency as the For Loop as discussed in the above method.

How To Find The Sum Of An Array Of Numbers in JavaScript

Wrap Up

I hope you were able to get your answer related to how to add an array of numbers in JavaScript. I have listed three methods that are most commonly used. And still, there are other methods but are not listed here since those are not worth using.

Let me know if you have any better methods than the one discussed above I will be happy to add them here.

If you liked the above tutorial then please follow us on Facebook and Twitter. Let us know the questions and answer you want to cover in this blog.

Further Read:

  1. How To Remove A Specific item From An Array
  2. Best Udemy JavaScript Course 2021
  3. Easiest and Best Way To Convert Int to String in C++
  4. How To Convert a Char to String in C++ Quickly
  5. Parse (split) or Tokenize a String in C++ Using Delimiter
How To Find The Sum Of An Array Of Numbers in JavaScript

Leave a Comment