2

In Javascript what do the square brackets mean for declaring a function. And how can you declare more arguments with a space? Is this psuedocode?

router.METHOD(path, [callback, ...] callback)

Source: https://expressjs.com/en/4x/api.html#router.METHOD

darkace
  • 838
  • 1
  • 15
  • 27
  • https://developer.mozilla.org/en-us/docs/Web/JavaScript/Reference/Operators/Spread_syntax – Nick Jan 26 '21 at 22:02
  • 1
    Does this answer your question? [Is it spread "syntax" or the spread "operator"?](https://stackoverflow.com/questions/44934828/is-it-spread-syntax-or-the-spread-operator) – eol Jan 26 '21 at 22:03
  • That's the documentation notation for optional arguments. – evolutionxbox Jan 26 '21 at 22:04
  • 1
    This notation is not part of the function declaration. It's a documentation convention. – Barmar Jan 26 '21 at 22:05
  • 1
    it is a formal description of optional parts with [EBNF](https://en.wikipedia.org/wiki/Extended_Backus%E2%80%93Naur_form). – Nina Scholz Jan 26 '21 at 22:06
  • Wow that was quick! How did you know that @Barmar and Nina Scholz? Is there a nice article that runs through some examples? – darkace Jan 26 '21 at 22:12
  • 1
    @darkace Because I've been programming for 40 years and I've seen it in hundreds of documentation examples. – Barmar Jan 26 '21 at 22:13
  • @NinaScholz tagging you – darkace Jan 26 '21 at 22:13
  • Do you know a good resource for getting more familiar with this @Barmar? I'm not entirely sure what to google here – darkace Jan 26 '21 at 22:17
  • Start with https://en.wikipedia.org/wiki/Extended_Backus%E2%80%93Naur_form – Barmar Jan 26 '21 at 22:18

1 Answers1

1

If this should be a comment, let me know.

In this particular instance, it's saying that you can pass in any number of middleware functions, separated by commas, *AND/OR* one single callback to be run as the last part of the method.

The square brackets sort of represent an "array" (though not really an array) of however many middleware functions you want to run before the final function call for the method.

The space after the square brackets is sort of an *AND/OR* character saying that you can pass however many middleware functions, AND a final function, OR just the final function without the middleware functions.

They're not declaring a function here, in the documentation. They're showing you "this is how many functions you can pass as middleware for this method which has been defined elsewhere: as many as you want, AND THEN the final function, or zero, AND THEN the final function"

Clear as mud?

R Greenstreet
  • 719
  • 6
  • 21
  • Great! Only thing missing is declaring that this is just part of documentation style rather than javascript code. And also what would be a good reference for this or phrase that I can google? Previous comments suggest EBNF but it's a bit abstract for my liking https://en.wikipedia.org/wiki/Extended_Backus%E2%80%93Naur_form – darkace Jan 26 '21 at 22:23
  • 1
    You've got me there, man. I don't know what the specific term for this is. However I would be inclined to say that it's not restricted to only programming. I've seen similar examples in a lot of plain English where what can be inserted varies. Sort of like MadLibs – R Greenstreet Jan 26 '21 at 22:31
  • Well, I'm glad to hear it isn't just me that's a bit stumped! Thanks for the great answer – darkace Jan 26 '21 at 22:36