6

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

Noob_Number_1
  • 725
  • 5
  • 20
  • 3
    APIs commonly use brackets to indicate optional parameters. It's not a language feature. – JJJ Mar 07 '15 at 12:04
  • 3
    The `square brackets` mean that the parameter is optional, hence the `,` is just seperating the `parameters`, like if you would make use of it the function would be `arr.reduce(callback, initialValue)`. As poster above mentioned it's not a `language` feature but the way of `documenting` it. – Avinor Mar 07 '15 at 12:04
  • English is not my language and I thought that the title doesn't mean that the syntax is JavaScript. I'll, fix it. And I still don't know why then is callback in the definition and not the rest of variables. And thanks by the way ;) – Noob_Number_1 Mar 07 '15 at 12:09
  • 1
    Ah ok. That's because for `callback` you refer to a `function` which has the parameters `previousValue`, `currentValue`, `index` and `array`. The `intialValue` is an optional `parameter` for the `reduce` function itself. If you look into the examples it says `.reduce(function(callback params))`. An `anonymous function` is specified in the `reduce` function. – Avinor Mar 07 '15 at 12:17
  • Ok. I thought that those parameters were for reduce and not for the callback. Now it's clear. Thank you very much ;) – Noob_Number_1 Mar 07 '15 at 12:19
  • @Francodi, just one thing more. So, in the second example, `index`, `currentValue`, etc. are internally defined and `reduce` calculate internally their values? And for callback, parameter names are reserved names? – Noob_Number_1 Mar 07 '15 at 12:28
  • @Noob_Number_1 You've copied the infomation from the page incorrectly. Under Parameters, under `callback`, there is no `initialValue`. –  Mar 07 '15 at 12:58
  • @torazaburo can you explain me this question: in the second example, index, currentValue, etc. are internally defined and reduce calculate internally their values? And for callback, parameter names are reserved names? – Noob_Number_1 Mar 07 '15 at 13:08
  • No, they're not reserved names. They just describe what the parameter does. You could use x, y, and z if you preferred. –  Mar 07 '15 at 15:17

2 Answers2

3

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.

Joseph
  • 117,725
  • 30
  • 181
  • 234
  • Thanks, I read that at the MDN article. What I don't understand is why is just `callback` at the definition and not the rest of `reduce` variables. – Noob_Number_1 Mar 07 '15 at 12:15
  • 1
    @Noob_Number_1 `callback` isn't optional. Try running reduce and not providing `callback` and the operation will throw an error. – Joseph Mar 07 '15 at 12:16
  • 2
    This is incorrect. If `initialValue` is omitted, the first iteration of `reduce` is **not** provided `null` as an argument. Instead, the *first two elements of the array* are passed as `previousValue` and `currentValue`. –  Mar 07 '15 at 12:57
1

As already explained in other answers, the parameters inside of the "[]" are optional. Regarding the question as to why the "other parameters" (i.e. previousValue etc.) are not there, those are parameters to callback and not to reduce. So callback will receive those arguments on each run of reduce.

Abhijit
  • 895
  • 5
  • 11