Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does this documentation syntax mean at MDN for a JavaScript function? [duplicate]

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

like image 705
Noob_Number_1 Avatar asked Oct 20 '22 16:10

Noob_Number_1


1 Answers

It's usually a convention for API documentations to use [] to denote optional parameters. However, the [] is not part of the syntax on usage. It's just documentation convention.

like image 157
Joseph Avatar answered Oct 22 '22 08:10

Joseph