Make sure the values are numbers, otherwise they will concat instead of suming.

Copya = parseInt(a, 10); // a is now int 
Answer from kjetilh on Stack Overflow
🌐
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() with an accumulator to sum all array elements. const numbers = [10, 20, 30, 40] const sum = numbers.reduce((acc, num) => acc + num, 0) // Result: 100 const prices = [19.99, 25.50, 12.75] const total = prices.reduce((total, price) ...
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.

🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Math › sumPrecise
Math.sumPrecise() - JavaScript - MDN Web Docs
console.log(Math.sumPrecise([1, 2])); // Expected output: 3 console.log(Math.sumPrecise([1e20, 0.1, -1e20])); // Expected output: 0.1 ... An iterable (such as an Array) of numbers.
🌐
JavaScript.info
javascript.info › tutorial › the javascript language › advanced working with functions › recursion and stack
Sum all numbers till the given one
function sumTo(n) { let sum = 0; for (let i = 1; i <= n; i++) { sum += i; } return sum; } alert( sumTo(100) ); ... P.S. Naturally, the formula is the fastest solution. It uses only 3 operations for any number n. The math helps!
🌐
Programiz
programiz.com › javascript › examples › add-number
JavaScript Program to Add Two Numbers
To understand this example, you should have the knowledge of the following JavaScript programming topics: JavaScript Variables and Constants · JavaScript Operators · We use the + operator to add two or more numbers. const num1 = 5; const num2 = 3; // add two numbers const sum = num1 + num2; ...
🌐
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 - An array in JavaScript is an object that allows you to store an ordered collection of multiple values under a single variable name and manipulate those values in numerous ways. In this article, you will learn how to calculate the sum of all the numbe...
🌐
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 - The reduce() method calculates the sum of the array of numbers by executing the “reducer” callback function on each element of the array. The accumulator argument is the value of the previous call of the function.
Find elsewhere
🌐
Tutorial Republic
tutorialrepublic.com › faq › how-to-find-the-sum-of-an-array-of-numbers-in-javascript.php
How to Find the Sum of an Array of Numbers in JavaScript
<script> var array = [1, 2, 3, 4, 5]; // Getting sum of numbers var sum = array.reduce(function(a, b){ return a + b; }, 0); console.log(sum); // Prints: 15 </script>
🌐
Programiz
programiz.com › javascript › examples › sum-natural-number
JavaScript Program to Find the Sum of Natural Numbers
// program to display the sum of ... // in each iteration, i is increased by 1 for (let i = 1; i <= number; i++) { sum += i; } console.log('The sum of natural numbers:', sum);...
🌐
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.
🌐
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 - The calculateSum() function takes an array as a parameter and calculates the sum of the numbers in the array.
🌐
Medium
medium.com › @sudhanshudeveloper › summing-numbers-in-javascript-with-different-techniques-d4ded9146160
Summing Numbers in JavaScript with Different Techniques. | by Sudhanshu Gaikwad | Medium
July 31, 2024 - This function uses a loop to sum the first n natural numbers. It initializes a variable sum to 0. The loop runs from 0 to n. In each iteration, it adds the current value of i to sum.
🌐
W3Resource
w3resource.com › javascript-exercises › javascript-math-exercise-17.php
JavaScript Math: Calculate the sum of values in an array - w3resource
July 11, 2025 - Test Data : console.log(sum([1,2,3])); console.log(sum([100,-200,3])); console.log(sum([1,2,'a',3])); Output : 6 -97 6 ... // Define a function named sum that calculates the sum of an array of numbers.
🌐
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!
🌐
Vultr Docs
docs.vultr.com › javascript › examples › find-the-sum-of-natural-numbers
JavaScript Program to Find the Sum of Natural Numbers | Vultr Docs
November 11, 2024 - The sum of natural numbers can be calculated in JavaScript using either an iterative approach with a for loop or a direct mathematical formula. The iterative method gives you a deeper understanding of loops and incremental summing, whereas the formula method provides a quick and efficient way to compute the sum for large values.
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › how-to-find-the-sum-of-an-array-of-numbers-in-javascript
How to Find the Sum of an Array of Numbers in JavaScript ? - GeeksforGeeks
July 23, 2025 - ... // Given array let a = [10, ... the number in each iteration sum += a[i]; } // Display the final output console.log("Sum of Array Elements: ", sum);...
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › how-to-sum-all-numbers-in-a-range-in-javascript
How to Sum all Numbers in a Range in JavaScript ? - GeeksforGeeks
July 23, 2025 - ... The loops in JavaScript can be used to iterate over the elements that fall inside the entered maximum and minimum numbers which can be added one by one to get the sum of all of them.
🌐
Educative
educative.io › answers › how-to-sum-up-all-the-numbers-in-a-range-in-javascript
How to sum up all the numbers in a range in JavaScript
Our program should accept an array of two numbers as input, for example, sumAll([1, 3]). Then, it should process the sum of all the numbers in the given range. It should be noted that both the numbers in the given range are inclusive, that is, ...
🌐
DEV Community
dev.to › sudhanshudevelopers › summing-numbers-in-javascript-with-different-techniques-dh5
Summing Numbers in JavaScript with Different Techniques. - DEV Community
March 6, 2025 - This function uses a loop to sum the first n natural numbers. It initialises a variable sum to 0. The loop runs from 0 to n. In each iteration, it adds the current value of i to sum.