Take the 2-minute tour ×

According to the ECMA-262 Specification, the following statements return 1:

eval("1;var a;")

Ensuring that:

The value of a StatementList is the value of the last value producing Statement in the StatementList.

Can you explain these different returns ?

eval("{}") // undefined
eval("var a={}; a;") // {}
eval("var a={};") // undefined

What is the difference between 1; and {}; ?

share|improve this question
This is a duplicate, FWIW. (It is just awkwardly hard to find such duplicates on SO :-/) –  user166390 May 28 '12 at 23:18
stackoverflow.com/questions/9943278/javascript-eval-expression (it's the same idea, but results in different observed semantics due to invalid syntax) –  user166390 May 28 '12 at 23:19

2 Answers 2

up vote 6 down vote accepted

Left alone, {} is interpreted as a block, not an object. It contains no statements, so does not affect the value of, say, eval("1;{}"). To force it to be interpreted as an object, you can use parentheses:

eval("1;({})"); // {}
share|improve this answer
I got it ! I didn't even think about the block thing, thanks ;) –  Loïs Di Qual May 28 '12 at 22:52
"({})" is sufficient... (for balance with the second part of the post, which does not have a 1;) –  user166390 May 28 '12 at 23:14

It looks to me like eval is interpreting {} as the delimiters of a code block, and therefore has no inherent value.

share|improve this answer

Your Answer


