Consider following code:

function sum() {
    var sum = 0;
    for (var i = 0; i < arguments.length; i++) {
        sum += arguments[i];
    }
    return sum;
}
var a = sum(5, -5, false, '-5', 5);
console.log(a);
What will be printed to the console?
Explanation
 0+5 = 5
5 + -5 = 0
0 + false = 0 // true converts in to 1 and false into 0.
0 + '-5' = '0-5' // number + string converts to string
'0-5' + '5' = '0-55'
 

typeof arguments === 'object'

2018 Mar 23, 9:36:27 AM

typeof arguments === 'object'

2018 Mar 23, 9:36:27 AM

@KP1976 thanks! improved explanation

2017 Dec 5, 3:27:18 PM

First is 0+5 = 5

2017 Nov 29, 11:00:40 AM

Follow CodeGalaxy

Mobile Beta

Get it on Google Play
Send Feedback