New course! Every coder should learn Generative AI!
Try a free lesson+ 4
Why the output of this code is 48?
var x = "2*2"; var y = 4; var z = eval(x + y); console.log(z); the output is 48, but why? I don't understand it.
2 Answers
+ 12
"2*2"+4
Since x is a string and y is an integer, in JS it will add the y to x as a string type.
=> x+y = "2*2"+4 = "2*24" //Adds y to x as a string type.
So that when you use the eval function on x+y, it'll output 48 to the console.
0
How 48