1

You often see this kind of syntax for describing methods:

Math.max([value1[,value2, ...]])

Function.prototype.call (thisArg [ , arg1 [ , arg2, … ] ] )

Why are parameters denoted like this using brackets and leading commas?

1
  • Are you asking where this syntax comes from (I can't remember right now the name) ? Then this question would probably be better fit for another SE site. Commented Jan 22, 2014 at 17:45

2 Answers 2

1

Brackets are used for argument specifications to indicate that the argument is optional.

This likely comes from the format used in UNIX/Linux man pages (although they may have borrowed that syntax from some other earlier source for all I know). The man page on man-pages has a description of how arguments should be represented (emphasis mine):

SYNOPSIS briefly describes the command or function's interface. For commands, this shows the syntax of the command and its arguments (including options); boldface is used for as-is text and italics are used to indicate replaceable arguments. Brackets ([]) surround optional arguments, vertical bars (|) separate choices, and ellipses (...) can be repeated.

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

Comments

0

Javascript doesn't have strict requirements for function parameters. functions can have as many parameters as you want to put in them. You can call a function that only has two parameters with 3 parameters and javascript will ignore the ones that aren't noted.

However, the order of the variables is still important. In other words, you can't use just the first and third parameters, if you want to use the third parameter you have to specify something for the second.

the square brackets mean the parameter is not required. The comma is just to tell you that you need a comma if you are going to specify a parameter there.

1 Comment

Sometimes they also denote that you can omit the second parameter - the function can detect by the type of the argument whether you gave the third parameter or the second. The nesting of the brackets is relevant…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.