DEV Community

Discussion on: Understanding Rest Parameter Syntax

Collapse
 
georgecoldham profile image
George • Edited

Out of curiosity, why are you choosing to use

function(...[arg1, arg2, arg3]) { //... }

over

function({ arg1, arg2, arg3 }) { //... }

?

Is there any technical differences or is this just syntactical preference?

Collapse
 
laurieontech profile image
Laurie

The first example is using the rest parameter, which is what the article is meant to explain.

The second example would not apply for the example above since it's only passing integers. It would work if the example looked like this though.

function testRest({ first }) {
  console.log(first);
}
testRest({ first: 1 });

However, the "infinite" number of arguments use case is not handled unless the arguments in question are already in an object and their key names are known. It's a very different piece of syntax.