+ 1
Can someone explain in layman's terms what 'eval' does to produce the answer 48? It's confusing
var x = "2*2"; var y = 4; var z = eval(x+y); alert(z);
2 Answers
+ 2
maybe its like this
x+y
"2*2"+4 and the operation are resolved in string and become
2*24
+ 2
eval interprets a string
when you do x + y Javascript converts the 4 to an string since x is an string.
they get concated resulting in "2*24" when this is evaluated it returns 48