JS offers the 'arguments' object inside of functions to represent all passed arguments. Is there any broadly accepted concept for when/where this should be used?

I've seen items like this in the 'wild' that seem like code smell to me:

function myFxn () {
  var myVar = arguments[0];
  // do something with myVar

this is a very simplified example, but it illustrates what seems to me to be a poor coding practice. I would prefer

function myFxn (myVar) {
  // do something with myVar

A colleague used it in a number of places where they only wanted to name the 'required' args ('required' by the implemenation ) but omit the 'optional' ones:

function myFxn (myReqVar) {
  var myOptVar = (arguments[1] !== undefined) ? arguments[1] : false;
  // do something with vars

This didn't seem wise on grounds of reduced readability and maintainability. It seems to me to create functions with confusing interfaces that must rely on the developer's knowledge of its intent or external documentation to understand and use.

Is this just a matter of opinion, or is there a preponderance of opinion on this?

  • anything asking for "best" is almost automatically to be answered with "there's no universal best". – jwenting Sep 30 '14 at 7:03
  • @jwenting This is true, and I considered not asking for that reason, but rather pointed the question at that issue: IS this merely opinion or is there a standard for the use of that element. As always I have great respect for this community, though and consider even a closed question an answer in itself. – gkl Sep 30 '14 at 11:49

I think you are right, the best practice would be to use parameters over var paramName = arguments[0] . It is cleaner, easier to read and it shows the intent better. As a general rule of thumb the arguments object should be used over params under a few scenarios:

There can be N number of params:

    function sum(){
    var total = 0;
    for (var i = 0; i < arguments.length; i++) {
        total += arguments[i];
    return total;

If you want to generically call other functions with the passed in arguments:

function other() {
    otherFunction.apply(this, arguments);


function other(arg1, arg2, arg3) {
    otherFunction.call(this, arg1, arg2, arg3);

Using reusable functions that use arguments:

Here is an example of a generic argument validating : https://codereview.stackexchange.com/questions/31593/throwing-error-if-settings-arguments-are-undefined-is-this-a-good-breakout-fun/31602#31602

| improve this answer | |
  • Thanks, these are good examples of the cases where 'arguments' is a good fit. – gkl Sep 30 '14 at 1:36

The arguments object is useful for mimicking the behavior or overloaded methods, or creating functions that accept arbitrary numbers of arguments. A good native example is the Array#push method:

var x = [];
x.push(1, 2);

Is the equivalent of:

var x = [];

A good example of mimicking method overloading is the jQuery on method:

$("#foo").on("click", function(event) {
    // process the click event when "#foo" gets clicked

$("#foo").on("click", ".bar", function(event) {
    // process click event when any element with "bar" as one of its
    // classes gets clicked inside "#foo"

It's the same method name (on) but the different numbers and types of arguments change the behavior of the method, giving you the impression that the on method has two overloads.

| improve this answer | |
  • Good examples; I hadn't thought of the jQuery example - nice! – gkl Sep 30 '14 at 1:36

Not the answer you're looking for? Browse other questions tagged or ask your own question.