+ 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);

1st Jul 2019, 1:12 PM
Jesse Brite
Jesse Brite - avatar
2 odpowiedzi
+ 2
maybe its like this x+y "2*2"+4 and the operation are resolved in string and become 2*24
1st Jul 2019, 1:22 PM
Taste
Taste - avatar
+ 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
1st Jul 2019, 1:52 PM
Anton Böhler
Anton Böhler - avatar