How to get the Sum of an Array of Numbers in JavaScript

Get the Sum of an Array of Numbers using Array.reduce() method.

To get the sum of an array of numbers:

  1. Use the reduce() method to iterate over an array.
  2. To get started, set the initial value in the reduce method to 0.
  3. On each iteration, add the current number to the sum of all previous numbers.

Example –


const theArray = [2, 55, 100];

const sum = theArray.reduce((accumulator, value) => {
  return accumulator + value;
}, 0);

console.log(sum);
Get the sum of an array of numbers with the Array.reduce() method
Get the sum of an array of numbers with the Array.reduce() method

Here’s what the above code is doing:

  1. The reduce() method takes two arguments: a callback function and an initial value.
  2. The callback function takes two arguments: an accumulator and the current value.
  3. The accumulator is like a total that reduce() keeps track of after each operation.
  4. The current value is just the next element in the array that reduce() is iterating over.
  5. The initial value sets the accumulator to 0.
  6. The callback function adds the current value to the accumulator.
  7. The reduce() method returns the final value of the accumulator.

Get the Sum of an array of numbers using a for…of loop.

To get the sum of an array of numbers:

  1. Declare a sum variable and set it to 0.
  2. Use the for…of loop to iterate over the array.
  3. On each iteration, add the value of the current element to the sum variable.

Example –


const theArray = [2, 55, 100];

let sum = 0;

for (const value of theArray) {
  sum += value;
}

console.log(sum);
Get the Sum of an array of numbers using a for...of loop.
Get the Sum of an array of numbers using a for…of loop.

Here’s what the above code is doing:

  1. We create an array called theArray.
  2. We create a variable called sum and set it to 0.
  3. We use a for...of loop to loop over theArray.
  4. We add the value of the current element to sum.
  5. We log the value of sum to the console.

ℹī¸ The for…of loop is a great way to loop over arrays.

Get the sum of an array of numbers using a for loop.

To get the sum of an array of numbers:

  1. Declare a sum variable and set it to 0.
  2. Use the for loop to iterate over the array.
  3. On each iteration, add the value of the current element to the sum variable.

Example –


const theArray = [2, 55, 100];

let sum = 0;

for (let index = 0; index < theArray.length; index++) {
  sum += theArray[index];
}
Get the sum of an array of numbers with a for loop.
Get the sum of an array of numbers with a for loop.

Here’s what the above code is doing:

  1. We create a variable called sum and set it equal to 0.
  2. We create a for loop that will run as long as the index is less than the length of the array.
  3. We add the value of the current index to the sum variable.
  4. We increment the index by 1.
  5. We repeat steps 2-4 until the index is no longer less than the length of the array.
  6. We log the sum variable to the console.
🗒ī¸ The reduce() method is the best way to get the sum of an array of numbers in JavaScript. A for...of or for loop would be less efficient.