+ 1

js eval gives some nasty output

I have recently began playing js challenges, and I've come across this code: var a = "1"; var b = "2*2"; console.log(eval(a+b)); The output of this code is 24, but I don't understand how it came to this output. By my logic, it would be like this: eval first evaluates the variable a which is a string and returns 1; then it evaluates b which is a string also, and returns 4; now, concatenating those strings, I think the result should be 14. Am I missing something here?

19th Feb 2020, 12:20 PM
Calin Onofrei
Calin Onofrei - avatar
3 Respostas
+ 5
a+b is evaluated first, string concatenation occurs and the string becomes "12*2" https://code.sololearn.com/WsRqTWz3hOs7/?ref=app
19th Feb 2020, 12:27 PM
Gordon
Gordon - avatar
+ 1
Thank you for clarifying it for me, Gordon!
19th Feb 2020, 12:46 PM
Calin Onofrei
Calin Onofrei - avatar
0
Welcome. Keep up 💪
19th Feb 2020, 12:53 PM
Gordon
Gordon - avatar