Find Sum of Array in JavaScript

Find Sum of Array in JavaScript | There are different ways to find the sum of array elements in JavaScript. We can find the sum of array elements if it contains homogeneous integer or floating point numbers. But if the array contains elements of different data types then we won’t be able to find the sum of the array.

Find the Sum of Array in JavaScript Using reduce() Function

The sum of an array’s elements can be obtained using JavaScript’s array.reduce(callback, initialValue) method. The array.reduce() function invokes the callback function once for each array element, passing the result of the most recent callback function call as the first parameter and the current array element as the second argument. The output of the array.reduce() method is the total number of elements in the array.

let array = [2, 4, 6, 5, 10];

let result = array.reduce((a, b) => {
  return a + b;
});

console.log("The sum of array elements:", result);

Output:-

The sum of array elements: 27

As an alternative, you can count all of the array’s elements using a “for” loop (see the example below). In this JavaScript Array sum example, the elements of the array are added together using the reduce() method. Below are more instances of summarizing a JavaScript array using descriptions.

Find the Sum of Array in JavaScript Using For loop

We can also use the for loop to add the array in JavaScript. This method involves adding each thing one at a time, iterating until the last item is added.

function sumOfArray(array) {
  let sum = 0; 

  /* Arrays are zero-index based.
   * Here array.length = 5, the initialization block is 0.
   * Last item is index 4 which is < 5
   */
  for (let i = 0; i < array.length; i += 1) {
    // take every item in the array and add it to the sum variable
    sum += array[i];
    // initial: sum = 0
    // iteration 1: 2 + 4 => sum = 6
    // iteration 2: 6 + 6 => sum = 12
    // iteration 3: 12 + 8 => sum = 20
    // iteration 4: 20 + -2 => sum = 18
    // iteration 5: 18 + -1 => sum = 17
    // iteration 6: 17 + 0 => sum = 17
  }

  return sum;
}

const array = [2, 4, 6, 8, -2, -1, 0];
console.log("Sum of array elements = " + sumOfArray(array));

Output:-

Sum of array elements = 17

Find the Sum of Array in JavaScript Using forEach()

We can also employ forEach loop to obtain the array addition in JavaScript. A built-in or array-like method is called forEach. As illustrated here, it is easy. Also see:- Get Random Element From Array in JavaScript

function sumOfArray(array) {
  let sum = 0;

  array.forEach((item) => {
    sum += item;
  });

  return sum;
}

const array = [2, 4, 6, 8, -2, -1, 0];
console.log("Sum of array elements = " + sumOfArray(array));

Output:-

Sum of array elements = 17

The “for” loop and the reduce() technique for adding an array of numbers are two of the loops available in JavaScript. Calculating the sum of the array benefits from the repeating nature of loops, which seek to consider each element one at a time. To add all the integers in an array in JavaScript, use the for-loop and reduce() functions, which we have provided the syntax and usage for.

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published. Required fields are marked *