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

9
  • 3
    APIs commonly use brackets to indicate optional parameters. It's not a language feature. Commented Mar 7, 2015 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. Commented Mar 7, 2015 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 ;) Commented Mar 7, 2015 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. Commented Mar 7, 2015 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 ;) Commented Mar 7, 2015 at 12:19

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

3 Comments

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 callback isn't optional. Try running reduce and not providing callback and the operation will throw an error.
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.
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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.