1

Why is the following a syntax error?

(()=>{console.log(1)}())

()=>{console.log(1)} is clearly(?) an expression that evaluates to a fat arrow function, and () "should" call it?

Presumably because it is ambiguous. What is the ambiguity?

Obviously, I realise the following works:

(()=>{console.log(1)})()
36
  • 2
    @SPlatten no, this is not the issue. Commented Feb 3, 2020 at 15:57
  • 3
    I really don't understand why people are downvoting this question. This is a legit question about JS parsing... Commented Feb 3, 2020 at 15:58
  • 3
    "What is the ambiguity?" - it is when the arrow function does not have a block body but a concise body. That's why you cannot use arrow functions as operands to many operators Commented Feb 3, 2020 at 16:22
  • 2
    @Ben In short, a = b = () => a = b = c is valid and has resolved ambiguity through precedence rules. a = () => b () () () is the same - and equal to a = () => { return b()()(); }, not to a = (() => b)()()(). This is why () => { b() } is not allowed in that position either, even if an arrow function with a block body wouldn't be ambiguous. Commented Feb 3, 2020 at 16:30
  • 2
    @Ben Yes, it's always parsed as an arrow function expression, with the same grammar rules as a concise-body one. Commented Feb 3, 2020 at 16:31

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.