I am curious as to why previousValue is always undefined in the following code when using .reduce on an array:
Code:
[2,2,2,3,4].reduce(function(previousValue, currentValue){
console.log("Previous Value: " + previousValue);
console.log("Current Value: " + currentValue);
},0)
Output:
Previous Value: 0 (index):
Current Value: 2 (index):
Previous Value: undefined (index):
Current Value: 2 (index):
Previous Value: undefined (index):
Current Value: 2 (index):
Previous Value: undefined (index):
Current Value: 3 (index):24
Previous Value: undefined (index):23
Current Value: 4
A fiddle can be found here: http://jsfiddle.net/LzpxE/
function
. This will be the value ofpreviousValue
on the next iteration.