Ben Aston
Ben Aston

Reputation: 55729

Concise arrow function body grammar

As I understand it, there are two kinds of arrow function body: concise and verbose.

()=>1 // Concise
()=>{} // Not concise?

Concise lacks braces and is an AssignmentExpression, and verbose is presumably a block, but I am not sure.

14.2 of the spec defines the grammar of arrow functions, but it appears to include braces in the definition of the concise body.

1.  ConciseBody[In]:
2.      [lookahead ≠ {]ExpressionBody[?In, ~Await]
3.      {FunctionBody[~Yield, ~Await]}

Where am I going wrong? Where is the "verbose" body grammar defined in the spec?

Upvotes: 3

Views: 591

Answers (2)

Bergi
Bergi

Reputation: 664444

According to the spec terminology, every ArrowFunction has a ConsiseBody, and then those are distinguished in short ExpressionBody ones and normal brace-wrapped FunctionBody ones.

The commonly used terminology in the JS world (like here at MDN) does however often distinguish between a "concise body" and a "block body", which makes more sense to me personally.

Upvotes: 3

Ed Lucas
Ed Lucas

Reputation: 7305

It looks like it's specifying two options under ConciseBody: either an ExpressionBody (no brackets) OR a FunctionBody wrapped in brackets.

The format for how the spec is written is explained under "Grammar Notation", https://tc39.es/ecma262/#sec-grammar-notation

Upvotes: 4

Related Questions