Why doesn't sum change?

  • 0
    'use strict';
    window.onload = function () {
        const arr = [10, 14, 35, 34, 12, 23, 5, 345];
    
        let sum = 0;
    
        const func = (currentValue) => {
            sum += currentValue;
            console.log(sum);
        }
    
        console.log(sum);
        arr.forEach(func)
    }


    JavaScript Anonymous, Jun 21, 2019

  • 1 Answers
  • 0
    What's wrong?

    0 is output by calling console.log (sum); before arr.forEach (func) .

    The rest is output during the execution of the func function.

    What doesn't suit you?
    Anonymous

Your Answer
To place the code, please use CodePen or similar tool. Thanks you!