Page 1 of 1

"eval" question

PostPosted: Mon Jul 30, 2018 5:00 am
by Xray
I may not be using it correctly, but here is what's happening....

eval("2 + 3") returns 5

Okay so far....

eval("2" + "3") returns 23

Seems strange, but I can live with that.

Now, here's the weird one:

eval("2 + 3" + "2 + 3") returns 37

Can anyone please explain WHY eval returns such an unexpected value? Is it a bug, or am I simply not understanding how it works?

Thanks

Re: "eval" question

PostPosted: Sun Aug 05, 2018 1:34 pm
by FRA32
the reason for this is because the operators inside the eval function are not actually part of the function itself, as the eval function only operates on strings. As such, eval("2+3"+"2+3") actually calculates eval("2+32+3") as the strings are concatenated, and 2+32+3 is 37

Re: "eval" question

PostPosted: Sun Aug 05, 2018 6:33 pm
by Xray
Thanks, FRA32! That explains the odd results I've been getting! :clap: