Eval function in js | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

Eval function in js

How does it actually work var a="1"; var b="2*2"; console.log(eval(a+b)); Output 24 If a=3 Output 64 Plz explain how eval works in case of values in strings like above example...

3rd May 2019, 7:11 PM
Rucha
Rucha - avatar
4 Answers
+ 11
Eval evaluates a JavaScript expression just like it would actually stand at this point. eval(a+b) will be processed as eval("1"+"2*2") both strings are added now so it'd look like eval("12*2") now the string will be evaluated: 12*2 = 24 if a=3: eval(3+"2*2") is equal to eval("32*2") which results in 32*2 = 64
3rd May 2019, 7:40 PM
Aaron Eberhardt
Aaron Eberhardt - avatar
+ 2
Thanks ! I was lost too 🤯😁
29th May 2020, 11:57 AM
C0f3t
C0f3t - avatar
+ 1
Thankyou
4th May 2019, 5:06 AM
Rucha
Rucha - avatar
+ 1
Good result
7th Jun 2020, 8:13 PM
Bobur Mirzaqulov
Bobur Mirzaqulov - avatar