"eval" question
3 posts • Page 1 of 1
"eval" question
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
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
-
Xray - Posts: 500
- Joined: Sun Jun 17, 2012 6:12 am
- Location: USA
Re: "eval" question
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
- FRA32
- Posts: 229
- Joined: Wed Dec 03, 2014 9:51 pm
Re: "eval" question
Thanks, FRA32! That explains the odd results I've been getting!
-
Xray - Posts: 500
- Joined: Sun Jun 17, 2012 6:12 am
- Location: USA
3 posts • Page 1 of 1
Who is online
Users browsing this forum: No registered users and 4 guests