+ 3
Can you explain me why the answer is 48?
It's a Challenge in JavaScript: var x="2*2"; var y=4; var z=eval(x+y); alert(z);
2 Answers
+ 20
yep, that one is a bit tricky... what actually happenes is that x and y get concatenated(because x is a string) first and then the resulting string is evaled:
var x = "2*2"
var y = 4
var z = eval(x+y) // = eval("2*24")
alert(z) // 48
+ 5
Rohan Karan I think your answer incorrect.
Base on your example, if we change 4 to 5, this will happen:
z= eval( "2*2" + 5)
z= 2*2 + 2*5
z= 410
But the correct answer should be 50. The correct logic should be:
z= eval( "2 * 2" + 5)
z = "2" * ("2" + 5)
z = "2" * "25"
z = 50
with number 4:
z = eval( "2 * 2" + 4)
z = "2" * ("2" + 4)
z = "2" * "24"
z = 48
I hope someone clarifies this eval behavior.



