This'd be exactly the job for reduce.

If you're using ECMAScript 2015 (aka ECMAScript 6):

Copyconst sum = [1, 2, 3].reduce((partialSum, a) => partialSum + a, 0);
console.log(sum); // 6
Run code snippetEdit code snippet Hide Results Copy to answer Expand

For older JS:

Copyconst sum = [1, 2, 3].reduce(add, 0); // with initial value to avoid when the array is empty

function add(accumulator, a) {
  return accumulator + a;
}

console.log(sum); // 6
Run code snippetEdit code snippet Hide Results Copy to answer Expand

Isn't that pretty? :-)

Answer from Florian Margaine on Stack Overflow
Top answer
1 of 16
1826

This'd be exactly the job for reduce.

If you're using ECMAScript 2015 (aka ECMAScript 6):

Copyconst sum = [1, 2, 3].reduce((partialSum, a) => partialSum + a, 0);
console.log(sum); // 6
Run code snippetEdit code snippet Hide Results Copy to answer Expand

For older JS:

Copyconst sum = [1, 2, 3].reduce(add, 0); // with initial value to avoid when the array is empty

function add(accumulator, a) {
  return accumulator + a;
}

console.log(sum); // 6
Run code snippetEdit code snippet Hide Results Copy to answer Expand

Isn't that pretty? :-)

2 of 16
1484

Recommended (reduce with default value)

Array.prototype.reduce can be used to iterate through the array, adding the current element value to the sum of the previous element values.

Copyconsole.log(
  [1, 2, 3, 4].reduce((a, b) => a + b, 0)
)
console.log(
  [].reduce((a, b) => a + b, 0)
)
Run code snippetEdit code snippet Hide Results Copy to answer Expand

Without default value

You get a TypeError

Copyconsole.log(
  [].reduce((a, b) => a + b)
)
Run code snippetEdit code snippet Hide Results Copy to answer Expand

Prior to ES6's arrow functions

Copyconsole.log(
  [1,2,3].reduce(function(acc, val) { return acc + val; }, 0)
)

console.log(
  [].reduce(function(acc, val) { return acc + val; }, 0)
)
Run code snippetEdit code snippet Hide Results Copy to answer Expand

Non-number inputs

If non-numbers are possible inputs, you may want to handle that?

Copyconsole.log(
  ["hi", 1, 2, "frog"].reduce((a, b) => a + b)
)

let numOr0 = n => isNaN(n) ? 0 : n

console.log(
  ["hi", 1, 2, "frog"].reduce((a, b) => 
    numOr0(a) + numOr0(b))
)
Run code snippetEdit code snippet Hide Results Copy to answer Expand

Speed Optimized

The reduce way is nice as it is easy to write and generally simple to understand, but if you are looking for speed (which is usually not a concern), use a simple for loop.

Copyconst numbers = [1, 2, 3, 4];
let sum = 0;
for (let i = 0; i < numbers.length; i++) {
  sum += numbers[i];
}
console.log(sum);
Run code snippetEdit code snippet Hide Results Copy to answer Expand

Non-recommended dangerous eval use

We can use eval to execute a string representation of JavaScript code. Using the Array.prototype.join function to convert the array to a string, we change [1,2,3] into "1+2+3", which evaluates to 6.

Copyconsole.log(
  eval([1,2,3].join('+'))
)

//This way is dangerous if the array is built
// from user input as it may be exploited eg: 

eval([1,"2;alert('Malicious code!')"].join('+'))
Run code snippetEdit code snippet Hide Results Copy to answer Expand

Of course displaying an alert isn't the worst thing that could happen. The only reason I have included this is as an answer Ortund's question as I do not think it was clarified.

🌐
ReqBin
reqbin.com › code › javascript › m81eb1ms › javascript-sum-array-example
How to get a sum of array elements in JavaScript?
November 24, 2023 - The sum of the array elements will be returned as the result of the array.reduce() method. Alternatively, you can find the sum of array elements using a "for" loop. In this JavaScript Array Sum Example, we use the reduce() method to get the ...
🌐
CoreUI
coreui.io › answers › how-to-sum-an-array-of-numbers-in-javascript
How to sum an array of numbers in JavaScript · CoreUI
September 29, 2025 - Use reduce() method to sum array elements efficiently, or loop through values for calculating totals in JavaScript.
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › how-to-find-the-sum-of-all-elements-of-a-given-array-in-javascript
Sum of an Array in JavaScript - GeeksforGeeks
July 23, 2025 - We are going to use the JavaScript reduce() method to find the sum of the array. reduce() is a more elegant way to sum up array elements. It works by processing all the array elements and combining them into a single value.
🌐
Sling Academy
slingacademy.com › article › javascript-ways-to-calculate-the-sum-of-an-array
JavaScript: 6 Ways to Calculate the Sum of an Array - Sling Academy
February 19, 2023 - Another way to sum the elements of an array for your reference (basically, it’s quite similar to other methods of using loops). ... const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; let sum = 0; let i = -1; while (++i < arr.length) { sum += arr[i]; } console.log(sum); ... Just another kind of loop in Javascript. Here’s how to make use of it to calculate the total value of a given array:
🌐
Medium
gemamr.medium.com › how-to-sum-array-value-in-javascript-7cf7e9bd3f87
How to Sum Array of Numbers in Javascript | by Gema | Medium
August 7, 2022 - Above is a function called arraySum, so what does this function do? basically, it only does a looping through the array that we send to its parameter, but not only looping, while the looping process we also calculate the total of the array value, that’s why we have variable numb and += operator there. If you’re new to Javascript, maybe you’re wondering what the += operator means, below is the explanation of what this operator means.
🌐
freeCodeCamp
freecodecamp.org › news › how-to-add-numbers-in-javascript-arrays
JS Sum of an Array – How to Add the Numbers in a JavaScript Array
March 31, 2023 - The second argument to the reduce() method is the initialValue, which is 0. The initialValue represents the initial value of the accumulator. To learn more about the reduce() method, give this article a read.
🌐
Sentry
sentry.io › sentry answers › javascript › how to find the sum of an array of numbers
How to find the sum of an array of numbers | Sentry
September 15, 2023 - ... const arr = [23, 34, 77, 99, ... sum of the array of numbers is calculated by looping through the array and adding the value of each array element to a variable called sum....
Find elsewhere
🌐
W3Resource
w3resource.com › javascript-exercises › javascript-math-exercise-17.php
JavaScript Math: Calculate the sum of values in an array - w3resource
July 11, 2025 - // Define a function named sum that calculates the sum of an array of numbers. function sum(input){ // Check if the input is an array, if not, return false. if (toString.call(input) !== "[object Array]") return false; var total = 0; // Iterate ...
🌐
Bobby Hadz
bobbyhadz.com › blog › javascript-get-sum-of-array-of-numbers
Get the Sum of an Array of Numbers in JavaScript | bobbyhadz
March 2, 2024 - Use the Array.reduce() method to iterate over the array. Set the initial value in the reduce method to 0. On each iteration, return the sum of the accumulated value and the current number.
🌐
Educative
educative.io › answers › how-to-get-the-sum-of-an-array-in-javascript
How to get the sum of an array in JavaScript
function sumArray(array) { let sum = 0; /*loop over array and add each item to sum */ for (const item of array) { sum += item; } // return the result console.log(sum); return sum; } sumArray([1, 4, 0, 9, -3]); //logs 11
🌐
Medium
medium.com › @onlinemsr › top-6-easy-ways-to-sum-an-array-in-javascript-41491c305fd2
How to Sum an Array in JavaScript: A Complete Guide | Medium
March 2, 2024 - Do you want to learn how to sum an array in JavaScript? If you are a web developer, you probably encounter this problem frequently, as arrays are one of the most common and useful data structures in JavaScript. However, unlike some other programming languages, JavaScript does not have a built-in method to sum an array of numbers.
🌐
EDUCBA
educba.com › home › software development › software development tutorials › javascript tutorial › javascript sum array
Javascript Sum Array | Calculate the Sum of the Array Elements
April 13, 2023 - The most recent, optimized, and efficient way of calculating the sum of array elements is the use the reduce() method in javascript and write a reducer function or callback for it that will return the accumulated value.
Address   Unit no. 202, Jay Antariksh Bldg, Makwana Road, Marol, Andheri (East),, 400059, Mumbai
🌐
Scaler
scaler.com › home › topics › how to find the sum of array in javascript?
How to Find the Sum of an Array in JavaScript? - Scaler Topics
April 25, 2024 - Here is the syntax: Here are the steps to be followed while calculating the sum using forEach: We declare a sum variable and initialize it to 0. We go through each item in the array using the forEach method.
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Array › reduce
Array.prototype.reduce() - JavaScript - MDN Web Docs
reduce() is a central concept in functional programming, where it's not possible to mutate any value, so in order to accumulate all values in an array, one must return a new accumulator value on every iteration. This convention propagates to JavaScript's reduce(): you should use spreading or other copying methods where possible to create new arrays and objects as the accumulator, rather than mutating the existing one.
🌐
Delft Stack
delftstack.com › home › howto › javascript › javascript sum of array
How to Sum of an Array in JavaScript | Delft Stack
March 4, 2025 - How do I sum an array of numbers in JavaScript? You can sum an array using a for loop, the reduce method, or the forEach method. What is the best method to sum an array? The best method depends on your coding style and project requirements.
🌐
W3docs
w3docs.com › javascript
How to Find the Sum of an Array of Numbers | W3Docs
You can use the reduce() method to find the sum of an array of numbers. The reduce() method executes the specified reducer function on each member of the array resulting in a single output value as in the following example: Javascript reduce ...
🌐
Squash
squash.io › how-to-sum-an-array-of-numbers-in-javascript
How To Sum An Array Of Numbers In Javascript - Squash Labs
October 15, 2023 - Finally, the function returns the calculated sum. Related Article: How to Navigate Using React Router Programmatically · Another approach to summing an array of numbers in Javascript is by using the reduce method.
🌐
Bobby Hadz
bobbyhadz.com › blog › javascript-get-sum-of-array-object-values
How to Sum a Property in an Array of Objects in JavaScript | bobbyhadz
March 2, 2024 - The calculateSum function takes an array and a property as parameters and calculates the sum of the specified property in the array of objects. If you need to sum the values of an object, check out the following article.
🌐
David Walsh
davidwalsh.name › sum-array-numbers
Sum an Array of Numbers with JavaScript
September 7, 2023 - Fear not -- summing an array of numbers is easy using Array.prototype.reduce! const numbers = [1, 2, 3, 4]; const sum = numbers.reduce((a, b) => a + b, 0); The 0 represents the starting value while with a and b, one represents the running total ...