JavaScript variable number of arguments to function

Just use the arguments object:

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

foo(111, 222, 333, 444);

// expected output:
// 111
// 222
// 333
// 444

In (most) recent browsers, you can accept variable number of arguments with this syntax:

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

foo(111, 222, 333, 444);

// expected output: 111 222 333 444

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top