JavaScript Program to Find Sum of Array Elements
You can find the sum of an array in JavaScript using
array.reduce((acc, val) => acc + val, 0), which adds all elements starting from zero.Examples
Input[1, 2, 3]
Output6
Input[10, -5, 7, 3]
Output15
Input[]
Output0
How to Think About It
To find the sum of numbers in an array, think of adding each number one by one to a total starting at zero. You go through the list from start to end, adding each number to your total until you finish all numbers.
Algorithm
1
Start with a total sum set to zero.2
Look at each number in the array one by one.3
Add the current number to the total sum.4
After all numbers are added, return the total sum.Code
javascript
const numbers = [1, 2, 3, 4, 5]; const sum = numbers.reduce((acc, val) => acc + val, 0); console.log(sum);
Output
15
Dry Run
Let's trace the array [1, 2, 3] through the code to see how the sum is calculated.
1
Initialize sum
Start with acc = 0
2
Add first element
acc = 0 + 1 = 1
3
Add second element
acc = 1 + 2 = 3
4
Add third element
acc = 3 + 3 = 6
5
Return result
Sum is 6
| Accumulator (acc) |
|---|
| 0 |
| 1 |
| 3 |
| 6 |
Why This Works
Step 1: Start with zero
We begin adding from zero because zero is the neutral number for addition, so it doesn't change the sum.
Step 2: Add each element
Each number in the array is added to the running total using the + operator.
Step 3: Use reduce method
The reduce method loops through the array and keeps updating the total sum automatically.
Alternative Approaches
for loop
javascript
const numbers = [1, 2, 3, 4, 5]; let sum = 0; for (let i = 0; i < numbers.length; i++) { sum += numbers[i]; } console.log(sum);
This is a simple and clear way but requires more lines than reduce.
for...of loop
javascript
const numbers = [1, 2, 3, 4, 5]; let sum = 0; for (const num of numbers) { sum += num; } console.log(sum);
This is more readable than a classic for loop and easy to understand.
while loop
javascript
const numbers = [1, 2, 3, 4, 5]; let sum = 0; let i = 0; while (i < numbers.length) { sum += numbers[i]; i++; } console.log(sum);
Works fine but less common for this task compared to for loops.
Complexity: O(n) time, O(1) space
Time Complexity
The program visits each element once to add it, so the time grows linearly with the array size.
Space Complexity
Only a few variables are used for the sum and counters, so space stays constant regardless of array size.
Which Approach is Fastest?
All methods have similar speed since they all add each element once; reduce is concise but for loops are equally efficient.
| Approach | Time | Space | Best For |
|---|---|---|---|
| reduce | O(n) | O(1) | Concise functional style |
| for loop | O(n) | O(1) | Classic and explicit control |
| for...of loop | O(n) | O(1) | Readable iteration |
| while loop | O(n) | O(1) | Less common, manual control |
Use
reduce for a clean and functional way to sum array elements.Forgetting to provide the initial value (0) to
reduce can cause errors with empty arrays.