Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unlimited arguments in a JavaScript function

Can a JavaScript function take unlimited arguments? Something like this:

testArray(1, 2, 3, 4, 5...); 

I am trying:

var arr = []; function testArray(A) {     arr.push(A); } 

But this doesn't work (output is only the first argument). Or the only way is:

function testArray(a, b, c, d, e...) {  } 

Thanks

like image 720
rhavd Avatar asked Jun 18 '11 12:06

rhavd


People also ask

How do you pass infinite arguments in JavaScript?

Basically, it is an array of parameters, which allows us to pass unlimited arguments in a JavaScript function. We created sum() function using rest parameters syntax. Inside of the function we used Array. reduce() method in combination with arrow functions, to sum an unlimited number of arguments.

How many arguments can a function have JavaScript?

When you call a function in JavaScript, you can pass in any number of arguments, regardless of what the function declaration specifies. There is no function parameter limit.

What is the maximum number of arguments a function can take?

Except for functions with variable-length argument lists, the number of arguments in a function call must be the same as the number of parameters in the function definition. This number can be zero. The maximum number of arguments (and corresponding parameters) is 253 for a single function.

What happens if you pass an extra parameter to a method in JavaScript?

Unless otherwise specified in the description of a particular function, if a function or constructor described in this clause is given more arguments than the function is specified to allow, the extra arguments are evaluated by the call and then ignored by the function.


2 Answers

There's a weird "magic" variable you can reference called "arguments":

function manyArgs() {   for (var i = 0; i < arguments.length; ++i)     alert(arguments[i]); } 

It's like an array, but it's not an array. In fact it's so weird that you really shouldn't use it much at all. A common practice is to get the values of it into a real array:

function foo() {   var args = Array.prototype.slice.call(arguments, 0);   // ... 

In that example, "args" would be a normal array, without any of the weirdness. There are all sorts of nasty problems with "arguments", and in ECMAScript 5 its functionality will be curtailed.

edit — though using the .slice() function sure is convenient, it turns out that passing the arguments object out of a function causes headaches for optimization, so much so that functions that do it may not get optimized at all. The simple, straightforward way to turn arguments into an array is therefore

function foo() {   var args = [];   for (var i = 0; i < arguments.length; ++i) args[i] = arguments[i];   // ... } 

More about arguments and optimization.

like image 105
Pointy Avatar answered Sep 22 '22 11:09

Pointy


As of ECMAScript 2015 (or ES6) we also have access to rest parameters that give us a slightly cleaner way to manage arguments:

function foo(a, b, ...others) {     console.log("a and b are ", a, b);      for (let val of others) {         console.log(val);     } }  foo(1, 2, 3, 4, 5); 

At the time of this writing, this is supported by Chrome 47+, Firefox 15+, and Edge. The feature is also available via both Babel and TypeScript transpiling down to ES5.

like image 27
Lucien Greathouse Avatar answered Sep 22 '22 11:09

Lucien Greathouse