Sum of Array Elements in JavaScript
Sum array elements in JavaScript with a for loop, reduce, forEach and for...of. Learn why reduce needs a 0 start value, with explanation and output.
1// Method 1: Using for loop2function sumArray(arr) {3 let sum = 0;4 for (let i = 0; i < arr.length; i++) {5 sum += arr[i];6 }7 return sum;8}910let numbers = [1, 2, 3, 4, 5];11console.log("Array:", numbers);12console.log("Sum:", sumArray(numbers));1314// Method 2: Using reduce15function sumArrayReduce(arr) {16 return arr.reduce((acc, num) => acc + num, 0);17}1819console.log("\nUsing reduce:", sumArrayReduce([10, 20, 30]));2021// Method 3: Using forEach22function sumArrayForEach(arr) {23 let sum = 0;24 arr.forEach(num => sum += num);25 return sum;26}2728console.log("\nUsing forEach:", sumArrayForEach([5, 10, 15]));2930// Method 4: Using for...of loop31function sumArrayForOf(arr) {32 let sum = 0;33 for (let num of arr) {34 sum += num;35 }36 return sum;37}3839console.log("\nUsing for...of:", sumArrayForOf([2, 4, 6, 8]));4041// Method 5: One-liner42const sum = arr => arr.reduce((a, b) => a + b, 0);4344console.log("\nOne-liner:", sum([1, 2, 3, 4, 5]));4546// Method 6: Sum with condition (even numbers only)47function sumEvenNumbers(arr) {48 return arr.filter(num => num % 2 === 0)49 .reduce((acc, num) => acc + num, 0);50}5152console.log("\nSum of even numbers:", sumEvenNumbers([1, 2, 3, 4, 5, 6]));
Sample Output
Array: [ 1, 2, 3, 4, 5 ] Sum: 15 Using reduce: 60 Using forEach: 30 Using for...of: 20 One-liner: 15 Sum of even numbers: 12
In this one
- Four ways to loop through an array
- How reduce and its accumulator work
- Why reduce needs a starting value
- How to filter before summing
How it works
- 1
sumArray([1, 2, 3, 4, 5]) returns 15.
- 2
reduce adds [10, 20, 30] to 60.
- 3
forEach adds [5, 10, 15] to 30.
- 4
for...of adds [2, 4, 6, 8] to 20.
- 5
filter keeps 2, 4, 6 and reduce adds them to 12.
Edge cases
Empty array without a start value
[].reduce((a, b) => a + b) throws a TypeError.
Always pass 0 as the start value when summing.
Strings in the array
[1, "2"] sums to "12".
Once a string appears, + joins text. Convert with Number().
Decimal prices
[0.1, 0.2] sums to 0.30000000000000004.
Use toFixed(2) for display, or work in paise as whole numbers.
What's going on
This program adds every number in an array. Think of a canteen bill stored as [40, 25, 15]. The total is what you pay.
Method 1 uses a classic for loop. sum starts at 0. Each round adds arr[i]. The loop runs while i < arr.length, so it stops after the last index.
Method 2 uses reduce: arr.reduce((acc, num) => acc + num, 0). reduce calls your function once per item. acc is the accumulator, the running total. The 0 at the end is the starting value.
That 0 is not decoration. If you leave it out, reduce uses the first item as the start. That works for non-empty arrays, but on [] it throws a TypeError: "Reduce of empty array with no initial value". With the 0, an empty bill simply sums to 0.
Method 3 uses forEach, which runs a function for each item. Method 4 uses for...of, which hands you each value directly with no index. It is often the easiest loop to read.
Method 5 is the reduce version as an arrow function. Method 6 chains filter and reduce. filter keeps only numbers where num % 2 === 0. Then reduce adds them. For [1, 2, 3, 4, 5, 6] the even numbers 2, 4, 6 add to 12.
Quiz
Tap an option. The answer stays hidden until you pick.
Question 1
What does [3, 4].reduce((a, b) => a + b, 0) return?
Pick an answer to check it.
Question 2
What happens with [].reduce((a, b) => a + b)?
Pick an answer to check it.
Questions people ask
Which loop should I use?
reduce is common in modern code. for...of is very readable. All give the same result.
Is forEach slower?
Slightly in some engines, but not enough to matter for normal arrays.
Can reduce do more than add?
Yes. It can multiply, build objects, count items and more.