Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does {a:1} evaluate to 1 in the console [duplicate]

I why does the console in Chrome and Firefox evaluate the current to 1:

> {a:1}
1

I would guess that it would be evaluated as an object like if you assign it to a variable:

> var a = {a:1}
undefined
> a
Object {a: 1}

And with quotes it throws an syntax error:

> {"a":1}
SyntaxError: Unexpected token :
like image 734
Andreas Louv Avatar asked Feb 17 '23 06:02

Andreas Louv


1 Answers

Try ({a:1}).

Just executing {a:1} is not what you think it is. It is not an object literal, which must be an expression (for example, on the right side of assignment).

Instead, what you have is a block, a label, and then a 1.

{
    a:
    1
}

Blocks return the result of their evaluation, and labels return the result of evaluating the statement that follows the label, so 1 is returned.

like image 129
josh3736 Avatar answered Feb 28 '23 11:02

josh3736