Why can't we omit parentheses from array destruction in array function parameters?

Viewed 66

Lets say I have the following codes:

  1. This example works

    ...
    .filter(([,second]) => console.log(second))
    
  2. This does not

    ...
    .filter([,second] => console.log(second))
    

Why do we have to wrap array destruction into parentheses? Isn't it boilerplate?

1 Answers

This is just the what the ES2015 spec states the behavior should be. The () are only optional if the parameter list is just a single simple parameter.

From the spec

ArrowFunction[In, Yield] :
     ArrowParameters[?Yield] [no LineTerminator here] => ConciseBody[?In]
ArrowParameters[Yield] :
     BindingIdentifier[?Yield]
     CoverParenthesizedExpressionAndArrowParameterList 

So the arrow parameters can be either a binding identifier or a CoverParenthesizedExpressionAndArrowParameterList

The CoverParenthesizedExpressionAndArrowParameterList is a regular parameter list (as seen here)

CoverParenthesizedExpressionAndArrowParameterList[Yield] :
     ( Expression[In, ?Yield] )
     ( )
     ( ... BindingIdentifier[?Yield] )
     ( Expression[In, ?Yield] , ... BindingIdentifier[?Yield] )

So the case when we can write a simple parameter is the BindingIdentifier case, which as seen here is just a simple identifier, so you can't use a de-structuring pattern:

BindingIdentifier[Yield] :Identifier
Related