+ 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

7th Dec 2021, 5:16 PM
Romjan Ali
Romjan Ali - avatar
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
7th Dec 2021, 6:06 PM
SoloProg
SoloProg - avatar
+ 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
7th Dec 2021, 7:01 PM
Solo
Solo - avatar