Reputation: 3164
If you type in the console
> new Object()
Empty js object will appear in the console and it is expected, but if you type
> {}
You get undefined
It is strange. Doesn't it ?
Upvotes: 1
Views: 311
Reputation: 414036
Calling a function with new
always results in a value. Now, your second line,
{}
is an empty code block, not an object literal.
Try:
({})
or:
0,{}
The console parses lines you type as statements, not expressions. An open curly brace at the start of a statement is a block delimiter, therefore, and not the start of an object literal. By including other tokens to force the parser into parsing an expression, you can then begin an object literal.
Upvotes: 6