March 11, 2007
Function Arguments Array
Inside every function in JavaScript there exists a contextual variable named arguments that acts like a pseudo-array. This object contains all the arguments passed in to the function. Arguments isn't a true array (meaning that you can't modify it, or call .push() to add new items), but you can access items in the array, and it does have a length property.
Even if you don't declare any argument names in your function declaration, you can actually pass one or more arguments when you call the function. This can be useful for writing functions that will accept any number of arguments.
Imagine we called a function with these arguments:
debate ("affirmative", "negative");
We could access those arguments via the arguments array inside the function:
function debate() { var affirmative = arguments[0]; var negative = arguments[1]; }
Or check to make sure a specific number of arguments were passed to the function:
function debate() { if ( arguments.length !=2 ) return false; }
This is something I just noticed and isn't normally mentioned in most beginner JavaScript books.
