Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript eval("{}") return behavior?

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

eval("1;;;;;")
eval("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 {}; ?

like image 323
ldiqual Avatar asked Feb 20 '23 08:02

ldiqual


1 Answers

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;({})"); // {}
like image 107
Ry- Avatar answered Feb 28 '23 02:02

Ry-