0

I keep seeing this type of notation in the syntax section of the MDN guide for Javascript:

arr.indexOf(searchElement[, fromIndex = 0])

(from https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/indexOf)

I see in the first example on page that:

array.indexOf(9, 2);  // 2 

so I understand that the brackets aren't actually part of JS syntax. Also I am guessing that the "fromIndex = 0" is a way of indicating that the default value for fromIndex is 0.

My question is:

Is there a handy guide to the notation used by MDN for representing generalized JS syntax?

I'm a beginner so it's hard to decide what is actual JS syntax and what is merely a way of describing the syntax. Hope this makes sense!

adeneo
  • 312,895
  • 29
  • 395
  • 388
FMD
  • 1
  • 1
  • 1
    When in doubt refer to the examples. The "syntax" is basically a representation of the parameters to pass into the function. Everything in square brackets [] is considered optional (and usually the default value is shown there) – UnholySheep Oct 24 '16 at 14:26
  • 1
    The square brackets are widely-adopted convention to indicate optional arguments. You find the same notation in other language documentation or in the help of command line tools. For example, if you're on Windows, try entering `dir /?` into the command line. On Linux, try `man find`, or the help page of virtually any other command, really. Square brackets are among *the* most recognized conventions. – Tomalak Oct 24 '16 at 14:28
  • the style is part of an extended [backus-naur form](https://en.wikipedia.org/wiki/Backus%E2%80%93Naur_Form) – Nina Scholz Oct 24 '16 at 14:41

0 Answers0