1

I don't know how to ask this question, but while i'm studying documentation of Javascript or Node.js they are defining syntax something like this.

var new_array = old_array.concat(value1[, value2[, ...[, valueN]]])

here all the parameters(value1[, value2[, ...[, valueN]]]) are defined as a nested array.

what actually it means, and

why not they are just defining something like this.

var new_array = old_array.concat(...values)

OR

var new_array = old_array.concat(valueList)

1
  • 2
    Any optional parameters are defined using comma , and square bracket [] Commented Mar 18, 2018 at 8:17

1 Answer 1

3

It has nothing to do with arrays (except that your example is taken from the documentation of a method on the array type). The square bracket means an optional argument and this is a convention used in most programming languages and tools. For example, the help for the ls command:

usage: ls [-ABCFGHLOPRSTUWabcdefghiklmnopqrstuwx1] [file ...]

... or cp:

usage: cp [-R [-H | -L | -P]] [-fi | -n] [-apvXc] source_file target_file
       cp [-R [-H | -L | -P]] [-fi | -n] [-apvXc] source_file ... target_directory

Your example means that the function takes at least one argument and more arguments can be added separated by a comma. According to the docs these values can be Arrays and/or values to concatenate into a new array. So, each valueN can be an array or a value to add to the returned array.

The reason not to use the ... notation is probably because the square brackets have been used for decades and are widespread, so readers will be familiar with the syntax no matter what the subject is.

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

Comments

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.