I need a method that can have an arbitrary number of parameters. In C# we have the params statement. Do we have anything similar in JavaScript?
I need a method that can have an arbitrary number of parameters. In C#
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
There is the
argumentscollection, which contains all arguments passed to the function.There is a) no need to specify “optional” arguments in the function signature and b) any function accepts any number of parameters.
Likewise, there is no need to supply “required” arguments in a function call:
Any argument named in the signature but not supplied in the function call will be
undefined.Note that
argumentsbehaves like an Array, but technically it isn’t one. For example, you can callarguments[0], but you can’t callarguments.slice(). What you can do to get around this is using the Array prototype:The so-called rest parameter
...is a new (ES6+) addition to the language and makes working with variadic functions more comfortable. @ArunCM’s answer explains it.