Cesar Jr Rodriguez
Cesar Jr Rodriguez

Reputation: 1821

What is the meaning of "...args" (three dots) in a function definition?

It was really confusing for me to read this syntax in Javascript:

router.route('/:id')
.put((...args) => controller.update(...args))
.get((...args) => controller.findById(...args));

What does ...args mean?

Upvotes: 55

Views: 114320

Answers (6)

Arun
Arun

Reputation: 1

It's called 'rest parameter', you can use rest parameter to pass unspecified number of arguments as an array, And a function can have only one rest parameter and it have to be the last parameter for the function

    function sum(...args){
    let output = 0;
     for(const num of args){
     output += num;
     }
        return output;
     }

     console.log(sum(2,4,8));

here it takes the argument that passed on sum as an array and sum the output and return it

Upvotes: 0

Akin Zeman
Akin Zeman

Reputation: 507

means pass all values (useful if have unknown# of items)

sample code

console.log(sum(1, 2, 3, 4));  // expected output: 10

function sum(...allItems) { 
  let total = 0;
  for (const item of allItems) {
     total += item;
   }
   return total;
}

Upvotes: 1

Rafiqi
Rafiqi

Reputation: 1

If you know some Python syntaxes, it is exactly the same as *args. Since *args (Python) is tuple object and Javascript has no tuple like Python, ..args is an Array object.

Upvotes: 0

Felix Kling
Felix Kling

Reputation: 816334

With respect to (...args) =>, ...args is a rest parameter. It always has to be the last entry in the parameter list and it will be assigned an array that contains all arguments that haven't been assigned to previous parameters.

It's basically the replacement for the arguments object. Instead of writing

function max() {
  var values = Array.prototype.slice.call(arguments, 0);
  // ...
}
max(1,2,3);

you can write

function max(...value) {
  // ...
}
max(1,2,3);

Also, since arrow functions don't have an arguments object, this is the only way to create variadic (arrow) functions.


As controller.update(...args), see What is the meaning of "foo(...arg)" (three dots in a function call)? .

Upvotes: 66

Ramesh
Ramesh

Reputation: 1887

The meaning of “…args” (three dots) is Javascript spread operator.

function sum(x, y, z) {
  return x + y + z;
}

const numbers = [1, 2, 3];

console.log(sum(...numbers));
// expected output: 6

Upvotes: 2

bejado
bejado

Reputation: 1460

Essentially, what's being done is this:

.put((a, b, c) => controller.update(a, b, c))

Of course, what if we want 4 parameters, or 5, or 6? We don't want to write a new version of the function for all possible quantities of parameters.

The spread operator (...) allows us to accept a variable number of arguments and store them in an array. We then use the spread operator again to pass them to the update function:

.put((...args) => controller.update(...args))

This is transparent to the update function, who receives them as normal arguments.

Upvotes: 18

Related Questions