+ 1

Need explanation for this JavaScript code

var x = "2*2"; var y = 4; var z = eval(x + y); alert(z); Output is 48 but I don't understand why.

9th Feb 2019, 11:14 AM
Nemanja Stepanović
5 Respostas
+ 5
1. It declares x with the string expression of "2*2" to indicate multiplication of 2 by 2 without actually solving it. 2. It declares y with the number 4. 3. It declares z. x + y is appending the y value (4) to the x ("2*2") because x is the string, making it a 2*24, and evaluates it, resulting in the answer being 48.
9th Feb 2019, 11:33 AM
EoflaOE
EoflaOE - avatar
+ 2
Now I understand, thank you ☺
9th Feb 2019, 1:51 PM
Nemanja Stepanović
+ 2
EoflaOE Thank you !
9th Apr 2019, 3:27 PM
codeKameleon
codeKameleon - avatar
+ 1
You are welcome!
9th Feb 2019, 4:37 PM
EoflaOE
EoflaOE - avatar
0
You are welcome
12th Apr 2019, 11:51 AM
EoflaOE
EoflaOE - avatar