Javascript Sum Array Reduce Object Values – Examples

There are different methods to sum the array this is very basic questions which comes up when starting with JavaScript. Below are the methods which we can use to sum the numbers in the array.

The methods include JavaScript sum array reduce method, JavaScript sum array foreach, JavaScript sum array of objects, JavaScript sum array for loop.

JavaScript Sum Array

Some of the examples which can help in JavaScript Sum Array

JavaScript sum array reduce()

var arr = [3, 4, 5, 6, 7];
 var sum = arr.reduce(function(a, b){
 return a + b;
}, 0);
console.log(“The sum of array is “+sum);
Output is The sum of array is 25

JavaScript sum array for loop

var array = [3, 4, 5, 6, 8];
var total=0;
for(var i in arr)
{
totalofnum += array [i];
}
console.log(“The sum of array is “+totalofnum);
Output is The sum of array is 26

JavaScript sum array of objects

var arr = [3, 4, 5, 6, 9];
var sum = 0;
for (var i = 0; i < arr .length; i++) {
sum += arr [i];
}
console.log(“The sum of array is “+sum);
Output is The sum of array is 27

JavaScript sum array foreach

var sum = 0;
arra.forEach(function (e) {
    sum += e;
});
 console.log(sum);
JavaScript More Topics

Was this helpful?

1 / 0

Leave a Reply 0

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


error: Content is protected !!