{} eval("{}") --> undefined Why is this? I...">

Eval ("{}") vs eval ("x = {}"), returns undefined while the others {}

eval("x={}")

  -->  {}

eval("{}")

  -->  undefined

      

Why is this?

I want to return an object from the evaluated string.

+3


source to share


3 answers


{}

parsed as an empty block statement, not an object literal expression.



To make it parse as an expression, wrap it in parentheses.

+9


source


{}

interpreted as a block with no statements, which evaluates to undefined

. x={}

force {}

evaluates to an expression (empty object) because you cannot assign a block to anything. The assignment operator then returns the value that was assigned.



A cleaner way to get the same effect is to copy the expression in parentheses. eval("({})")

gives the correct result for me.

+4


source


Eval ("{} + {}")
JavaScript has a lot of bugs.
Source: https://www.destroyallsoftware.com/talks/wat

0


source







All Articles