How to Add All Numbers in An Array JavaScript?
Tags: javascript, learning, webdev, tutorial
Published: 2024-05-15 15:06:04

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:
```javascript
// 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']; ❌
```

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:

```javascript
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
```

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.](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reduce)

###2. Using a For Loop:

In the second method, I'm using [for loop](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for) for adding All Numbers in An Array.

```javascript
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
```

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.

```javascript
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

```

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.