I was testing few scenarios with array reduce function.
[1,2].reduce(function(initial , val){
return initial+val;
} ,1)
// returns 4 as expected
[1,2].reduce(function(initial , val){
return initial+val;
})
// returns 3
But explicitly passing undefined as initial value
[1,2].reduce(function(initial , val){
return initial+val;
}, undefined)
// returns NaN.
It appears unusual to me.
arr.reduce(callback[, initialValue])where initial value is used as the first argument to the first call of the callback.undefinedwith a number try[1,2].reduce(function(initial , val){ initial = initial?initial:0 return initial+val; }, undefined)