1

I notice method signatures in JavaScript sometimes have multiple parameters like this:

let json = JSON.stringify(value[, replacer, space])

To me, it looks like this is a grammar that says replacer is an index for value, but this surely isn't the case. What does [, mean?

Chandan
  • 571
  • 4
  • 21
Jonathan Ma
  • 353
  • 3
  • 11
  • 5
    It means those parameters are optional. – Barmar Mar 05 '20 at 22:22
  • 3
    That's just used in documentation, it's not actual JavaScript code. – Barmar Mar 05 '20 at 22:22
  • Thanks a bunch. I notice also in D3.js, usage like this: ```dispatch.call(type[, that[, arguments…]])```. It looks nested - is it different in meaning? – Jonathan Ma Mar 05 '20 at 22:24
  • 3
    No, it's the same. Just different styles. – Barmar Mar 05 '20 at 22:25
  • 1
    @Barmar it could distinguish whether the two arguments can be omitted individually or only together. (But that would be wrong in this case) – Bergi Mar 05 '20 at 22:27
  • 1
    The D3 style makes it clear that each parameter is optional. The MDN style could be interpreted as meaning that if you supply `replacer` you also have to supply `space`. But that's not actually true. – Barmar Mar 05 '20 at 22:27
  • @Bergi It could be used that way, but MDN isn't doing that. – Barmar Mar 05 '20 at 22:28

1 Answers1

2

It's more for documentation of function arguments, and not part of syntax of the language. It means everything inside the [] including the comma, is optional arguments. Sometimes even, the parameters without commas are in brackets like so

someMethod(a, [b], [c])

But it's the same thing mostly.

incapaz
  • 349
  • 1
  • 3
  • 11