Why the output of this code is 48? | Sololearn: Learn to code for FREE!
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.

4th Oct 2023, 7:28 AM
Fatemeh Sadat Sanati
Fatemeh Sadat Sanati - avatar
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.
4th Oct 2023, 7:59 AM
Dragon RB
Dragon RB - avatar
0
How 48
5th Oct 2023, 7:26 PM
Dasarath Singh