JavaScript variable number of arguments to function

0

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

0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x
CodeROG
Logo
Reset Password
Compare items
  • Total (0)
Compare
0
Shopping cart