473,386 Members | 1,738 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Eval, Why does it do what it does?

Okay, recently for class, we ran into the following problem:

When we entered in
var x=1
eval(x + "2*2")
and had it returned and printed to the screen, we came out with 24 as the result. This defies all logic that we can think of, and so we decided to ask for help. When each section is done individually within the eval function, we get x is the value of 1, 2*2 is the value of 4, but when they are added, it always comes out as 24. Anybody out there know why?
Feb 28 '08 #1
3 1116
rnd me
427 Expert 256MB
Okay, recently for class, we ran into the following problem:

When we entered in
var x=1
eval(x + "2*2")
and had it returned and printed to the screen, we came out with 24 as the result. This defies all logic that we can think of, and so we decided to ask for help. When each section is done individually within the eval function, we get x is the value of 1, 2*2 is the value of 4, but when they are added, it always comes out as 24. Anybody out there know why?
sure, 12 * 2 = 24.

what's not to understand?

seriously though, here is the explination.

you are adding a number to a string.
remembering back to math class: PEMDAS, or parenthesis go first.
thus, it will add the number x to the string, and then evaluate the combined expression.

the "+" operator will only perform a mathematical addition if both sides are numbers. if one or both sides are strings, it is not an addition operator, it is a string concat operator,

so what you are doing is
1: 1+"2*2" // = "12*2"
2. eval ( "12*2" ) // = 24


does that help?
Feb 28 '08 #2
sure, 12 * 2 = 24.

what's not to understand?

seriously though, here is the explination.

you are adding a number to a string.
remembering back to math class: PEMDAS, or parenthesis go first.
thus, it will add the number x to the string, and then evaluate the combined expression.

the "+" operator will only perform a mathematical addition if both sides are numbers. if one or both sides are strings, it is not an addition operator, it is a string concat operator,

so what you are doing is
1: 1+"2*2" // = "12*2"
2. eval ( "12*2" ) // = 24


does that help?
Yes, thank you so much. That makes complete sense
Feb 29 '08 #3
you should not need to use eval ever anyway. there are easier ways to covert type string to number
Mar 2 '08 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: Reply Via Newsgroup | last post by:
This might sound sad... someone requesting a disertation on the 'eval' statement... but... I've been reading someone else's post - they had a huge calander like script and a handful of folk cursed...
11
by: sneill | last post by:
I have read a number of posts on the use of eval() in Javascript, and I agree that its use is questionable. But it does beg the following question: "How arbitrary does a string need to be before...
0
by: Michelle Keys | last post by:
Subject: DataBinder.Eval Error! Server Error in '/MSPOS' Application. ------------------------------------------------------------------------ -------- DataBinder.Eval:...
18
by: Joe Fallon | last post by:
I have some complex logic which is fairly simply to build up into a string. I needed a way to Eval this string and return a Boolean result. This code works fine to achieve that goal. My...
24
by: Larry | last post by:
Hi there: I have seen numerous postings about eval() and its evils on this forum. However, one of our developers is using it in the following way, which seems like a great use of it. Page...
15
by: manstey | last post by:
Hi, I have a text file called a.txt: # comments I read it using this:
8
by: fredo | last post by:
This question was asked in comp.lang.javascript with no result. In IE5.x and IE6, I want to display an image when the user rolls over a text link. The image does indeed display, but only on the...
4
by: Jm lists | last post by:
Hello members, I want to know does the "eval" in python have the same features as in Perl (capture errors)? For example,in perl I can wrote: $re = eval { 1 / 0 }; Though 1/0 is a fatal...
6
by: RandomElle | last post by:
Hi there I'm hoping someone can help me out with the use of the Eval function. I am using Access2003 under WinXP Pro. I can successfully use the Eval function and get it to call any function with...
10
by: Gordon | last post by:
I have a script that creates new objects based on the value of a form field. Basically, the code looks like this. eval ('new ' + objType.value + '(val1, val2, val3'); objType is a select with...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.