batman
batman

Reputation: 3685

Extra , is allowed for Destructuring assignments

In es6, the following seems to be valid code:

function test(a1,{a=1,b=2} = {},) {}

note the extra , in function args. I'm not sure whether this is a bug because this extra , is accepted only for Destructuring assignments.

Upvotes: 0

Views: 40

Answers (1)

T.J. Crowder
T.J. Crowder

Reputation: 1074038

As of ES2017, a trailing comma on parameter lists is valid regardless of destructuring. (See the spec for FormalParameters, which specifically lists FormalParameterList[?Yield, ?Await] , as one of the valid options.) For instance, this works just fine on an ES2017-compliant JavaScript engine like V8 in any recent(ish) version of Chrome:

function foo(a, b,) {
  // ------------^
  console.log(a, b);
}
foo(1, 2);

If you're seeing an error on that comma when you're not destructuring but not when you are, it's just that the JavaScript engine you're using isn't yet quite up to the current specification (yet).


Similarly, a trailing comma in argument lists is also allowed in ES2017+:

function foo(a, b) {
  console.log(a, b,);
  // -------------^
}
foo(1, 2);

Upvotes: 6

Related Questions