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 menthods which we can use to sum the numbers in the array.

    The menthods 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

    Leave a Reply

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