Why {a: 1} evaluates to 1 on the console

Why does the console in Chrome and Firefox evaluate the current value to 1 :

 > {a:1} 1 

I would suggest that it will be evaluated as an object, for example, if you assigned it to a variable:

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

And with quotes, it throws a syntax error:

 > {"a":1} SyntaxError: Unexpected token : 
+4
source share
1 answer

Try ({a:1}) .

Just executing {a:1} is not what you think. This is not a literal of an object that should be an expression (for example, on the right side of the destination).

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

 { a: 1 } 

Blocks return the result of their evaluation, and labels return the result of the evaluation of the operator following the label, therefore 1 returned.

+7
source

Source: https://habr.com/ru/post/1485512/


All Articles