+ 1
How to solve eval() in JavaScript?
When I have tried to solve this problem, I don't understood. Plz anyone inform me about this problem. var a = "1"; var b = "2*2"; console.log(eval(a+b)); Ans: 24
2 Réponses
+ 1
It stringed the 1 and 2 together and did 12 * 2 that is how you got the result.
eval(a+b) = eval("12*2") = 24
+ 2
a and b are string variables, so the "+" operator is redefined to the string concatenation operator:
eval(a+b) => eval("1"+"2*2") => 24