Why is `function () {{` legal in JS?

I recently saw some JS forms:

function f(x) {{ return x + 1; }}

      

To my surprise, this is syntactically legal and works great. At first I thought they were C-style anonymous scopes, but it doesn't introduce a new scope:

function f(x) {{ var y = x + 1; } return y;} // no error

      

Why does JS accept these extra parentheses? How are they interpreted / what do they mean?

+3


source to share


1 answer


{}

simply denotes a block or "group" of code. If paired with something like an operator if

it just doesn't do anything. The scope is var

defined function

in Javascript, not in blocks.



New let

in ES6 for blocks.

+3


source







All Articles