2
function [name]([param1[, param2[, ..., paramN]]]) {
  statements
}

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/function

I am confused what and how to read the above function input values, it is a infinite params input, but don't understand the used of brackets[], why put those.

Manasvi Sareen
  • 916
  • 7
  • 17
  • 2
    The `[]` mean those parameters are optional, meaning you do not have to pass those when calling the function – Patrick Evans Aug 10 '21 at 20:24
  • In the case of a dynamic language syntax like JavaScript, and in this document [] with a literal inside is a named token. [name] corresponds to the optional functionName token, as @vanowm poined out creates an anoymous unnamed function. [param1] corresponds to the first function formal parameter. These are names for the interpreter to consume and check for syntax correctness. – Vahe Aug 10 '21 at 20:28

1 Answers1

2

Square brackets represent optional

So it means, that [name] is optional, function declared without name become an anonymous function. [param1[, param2[,... this means that the parameters are optional, however because they are shown inside previous parameter (all closing ] brackets are at the end of the list) means that you can't have param2, without param1, or param3 without param2 and param1 and so on.

vanowm
  • 9,466
  • 2
  • 21
  • 37