2 ms·
More precisely, JavaScript doesn't have block scope. It does have syntax for grouping multiple statements into "blocks" (not sure if that's the right word), e.x
by tlrobinson 16y ago
More precisely, JavaScript doesn't have block scope. It does have syntax for grouping multiple statements into "blocks" (not sure if that's the right word), e.x.
if (...) {
// block
}
It turns out that blocks don't need to follow a control flow construct like "if", "while", etc, they can stand on their own anywhere a statement can.
What's happening in the above case is the syntax for an object literal and a block is ambiguous. It's normally not a problem since you don't have object literals standing on their own as statements, you do something with them as an expression. On a console (or any time you eval an expression) you should wrap it in a parenthesis to disambiguate it:
({x:1}) // expression (object with "x" property equal to "1")
{x:1} // statement (label "x" followed by statement "1")