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

We will be happy to hear your thoughts

Leave a reply

CodeROG
Logo
Compare items
  • Total (0)
Compare
0