24

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?

1
  • 2
    @Marcin, did not find anything about params, you? Commented Nov 29, 2011 at 11:40

6 Answers 6

33

There is the arguments collection, 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.

function foo() {
  console.log(arguments);
}

foo(1,2,3,4);  // logs [1, 2, 3, 4]

Likewise, there is no need to supply "required" arguments in a function call:

function foo(a, b, c, d) {
  console.log(arguments);
}

foo(1,2);  // logs [1, 2]

Any argument named in the signature but not supplied in the function call will be undefined.

Note that arguments behaves like an Array, but technically it isn't one. For example, you can call arguments[0], but you can't call arguments.slice(). What you can do to get around this is using the Array prototype:

Array.prototype.slice.call(arguments, 1, 2);

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.

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

Comments

20

I know this thread is too old but I believe something is missing here.

There is Rest parameter (introduced in ECMAScript 6) which will allow us to represent an indefinite number of arguments as an array.

It always returns an array. Which means even in defensive JavaScript land, it’s ok to do things like check .length of rest without guards.

Syntax :

function(a, b, ...theArgs) {
// ...
}

There are three main differences between rest parameters and the arguments object:

  1. rest parameters are only the ones that haven't been given a separate name, while the arguments object contains all arguments passed to the function
  2. the arguments object is not a real array, while rest parameters are Array instances, meaning methods like sort, map, forEach or pop can be applied on it directly;
  3. the arguments object has additional functionality specific to itself (like the callee property).

Additional reading : Spread

function f(x, ...y) {
  // y is an Array
  return x * y.length;
}

console.log("Expected result : 3*2 = 6 & Actual result : " + f(3, "hello", true));
console.log("Expected result : 3*4 = 12 & Actual result : " + f(3, "a", true, "b", 1));

//here we are not passing anything to "y" but its still safe to check .length of "y" because it always return an array.
console.log("Expected result : 3*0 = 0 & Actual result : " + f(3));

2 Comments

I think this should be the expected answer cause it's the true parallel to params (even though not all of us can use ES6)
@shirbr510 Just keep in mind that you can pass in an array instead of multiple separate objects in C# and the behavior will be the same. Where as if you pass an array into a js function using the spread operator instead of multiple separate objects the behavior will change. This is due to the static vs dynamic constraints of each language.
3

Yes. arguments.

function concatStrings () {
    var str = '';
    for (var i = 0; i < arguments.length; i++) {
        str += arguments[i];
    }
    return str;
}

Be aware that arguments isn't an array, so it doesn't have methods like join or push. It's just an array-like object (with numerical properties and a length property) so it can be iterated through.

Comments

2

JavaScript has arguments object inside functions. It contains of all params passed to the function.

More info

Comments

1

It is some sort of implicit in the special variable "arguments". Use like this:

function something(arg1, arg2) {
    for (var i = 0; i < arguments.length; i++) {
        var x = arguments[i];
    }
}

Then you can call it like something(1, 2, 3, 'a', 'b', 'c')

More examples here: http://www.jtricks.com/javascript_tutorials/varargs.html

Comments

0

Javascript functions can accept any number of parameters by default. You can see them with the arguments variable.

See here.

2 Comments

It's arguments, and it's not a keyword.
Whoops, my bad, that's what happens when you're doing three things at once. Edited.

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.