DEV Community

Cover image for How to Add All Numbers in An Array JavaScript?
Faruk Sardar
Faruk Sardar

Posted on

How to Add All Numbers in An Array JavaScript?

Many people who are learning JavaScript face problems when they need to sum all the numbers in an array.

The good thing is that there are many ways to Add All Numbers in An Array of JavaScript and I'm going to share the best 3 methods in this post.

Before you start following the tutorial, double-check that all the things in your list that you want to add up are numbers, not words.

Here is an example:

// Array containing numbers in number format
const numberArray = [1, 2, 3, 4, 5, 6]; 

// Array containing numbers in string format
const string array = ['1', '2', '3', '4', '5', '6']; 
Enter fullscreen mode Exit fullscreen mode

If the numbers in your list are written as words, you have to change them into numbers before doing what I said in my earlier message.

If your Array contains numbers in string format then it will throw an array while following this method.

1. Using Array.reduce() Method (Recommended):

In the first method, I'm going to use the reduce() method on the array to sum the items in the array.

This method will iterate over an array and perform a reduction operation on its elements

Here is how the function works:

function sumArrayWithReduce(arr) {
    return arr.reduce((acc, current) => acc + current, 0);
}

// Example usage:
const numbers = [1, 2, 3, 4, 5];
console.log(sumArrayWithReduce(numbers)); // Output: 15
Enter fullscreen mode Exit fullscreen mode

If you see the sumArrayWithReduce I have passed 0 as the second argument to the reduce() method, this means the first number it will sum is 0 then move on to the second number.

If I do not pass zero then the function adds an extra number which is present in the first item in the array.

You can learn more about reduce() on Mozilla.

2. Using a For Loop:

In the second method, I'm using for loop for adding All Numbers in An Array.

function sumArrayWithForLoop(arr) {
    let sum = 0;
    for (let i = 0; i < arr.length; i++) {
        sum += arr[i];
    }
    return sum;
}

// Example usage:
const numbers = [1, 2, 3, 4, 5];
console.log(sumArrayWithForLoop(numbers)); // Output: 15
Enter fullscreen mode Exit fullscreen mode

First I have created a function and inside the function, I declare a variable and set its value to 0.

Now I have created for loot and set it to run for the length of the array.

The for loop will iterate over the array you provide and add each of the numbers one by one.

3. Using forEach() Method:

You can also use the forEach method instead of the for loop,

The method is the same as the second method the only difference is that forEach will iterate over the array instead of the for loop.

Here's how you can put the code into action.

function sumArrayWithForEach(arr) {
    let sum = 0;
    arr.forEach(num => {
        sum += num;
    });
    return sum;
}

// Example usage:
const numbers = [1, 2, 3, 4, 5];
console.log(sumArrayWithForEach(numbers)); // Output: 15

Enter fullscreen mode Exit fullscreen mode

JavaScript gives you many options for doing the same tasks, and this applies to adding all numbers in an array too.

I suggested an easier method for doing this. Please tell me which one you used in your project among all three options.

Top comments (2)

Collapse
 
oculus42 profile image
Samuel Rouse

Hello! Nice article! As requested, I use Array.prototype.reduce() for this sort of operation.

function sumArrayWithReduce(arr) {
  return arr.reduce((sum, num) => sum + num, 0);
};
Enter fullscreen mode Exit fullscreen mode

The zero at the end is the starting value, and it is optional. If no starting value is provided, the first array value is passed. We can break this down into two very small, clear operations: add and sum:

const add = (a, b) => a + b;
const sum = (arr) => arr.reduce(add);
Enter fullscreen mode Exit fullscreen mode

Hope you enjoyed my method!

Collapse
 
thefaruksardar profile image
Faruk Sardar

Your method is great for beginners, Thanks for mentioning