What is the meaning of "... args" in the argument of a Javascript function?


Reading a bit of Javascript code, I've come across this:

.put((...args) => controller.update(...args))
.get((...args) => controller.findById(...args));

Is anyone clear about what ... args means?

asked by Cesar Jr Rodriguez 12.02.2017 в 20:07

2 answers


As a colleague commented to your question, they are Parameters Rest , in where simply this ( ...args ) is an object of type array ranging from 0 to args.length , example:

function fun1(...args) {

fun1();        // 0
fun1(5);       // 1
fun1(5, 6, 7); // 3
answered by 12.02.2017 / 20:28

Complementing what @Naos points out, the "ellipsis" are the Spread Operator , which allows you to specify that the function will treat what is contained in that parameter as an array, or more precisely an iterable one.

Formerly that was done using arguments and in your question it would behave exactly the same: an array with all the parameters delivered to the function.

However, the spread operator also allows to do things that previously required an elaborate return by applying array_slice on arguments. For example, declare a function that you know its first parameter, but do not know all the rest:

function marcavehiculo(vehiculo, ...marcas) {
   console.log('Quiero comprar un '+vehiculo+' de alguna de las siguentes marcas');

answered by 13.02.2017 в 20:01