I'm learning JavaScript through "Eloquent JavaScript" manual and now I'm doing exercises from chapter 5 "High-Order Functions". One of the functions this chapter introduces you is "reduce". I understand how it works, my problem comes up when I try to understand its definition at MDN. I don't understand syntax definition it gives:
arr.reduce(callback[, initialValue])
This syntax sections is followed by the section called Parameters. These are:
- callback
- previousValue
- currentValue
- index
- array
- initialValue (optional)
What I don't understand is what do those square brackets and the comma mean? Because when I see square brackets immediately I think in arrays. And why is just initialValue
in the definition and not the others parameters? Why there is no space between square brackets and callback
?
Because below there are two examples:
Example 1
[0, 1, 2, 3, 4].reduce(function(previousValue, currentValue, index, array) {
return previousValue + currentValue;
});
Example 2
var total = [0, 1, 2, 3].reduce(function(a, b) {
return a + b;
});
// total == 6
and I don't know how do they fit into the definition.
Thanks