Could anybody please tell me what is the use of eval function inside javascript code .
When i googled for this , i found the below from w3schools.comThe eval() function evaluates and/or executes a string of JavaScript code.
First, eval() determines if the argument is a valid string, then eval() parses the string looking for JavaScript code. If it finds any JavaScript code, it will be executed.
I found out using eval or not using eval isn't making any difference (Please correct me if i am wrong)
Sample Program using eval :
Same Program without using eval :
Please suggest , thanks .
Save India From Corruption - Anna Hazare.
Lester Burnham
Rancher
Joined: Oct 14, 2008
Posts: 1337
posted
0
I found out using eval or not using eval isn't making any difference (Please correct me if i am wrong)
You are wrong ... because that's a bad example. If the string to be evaluated is hardcoded in the page, then it really doesn't make sense to use eval. But generally the string would be dynamic - maybe it's retrieved from the server, or maybe it depends on user input.
But generally the string would be dynamic - maybe it's retrieved from the server .
When i used AJAX and used xmlhttprequest.responseText , i could get the value without using eval also , so is there any special scenario of where i should using eval function ??
Or please share any such scenario , thanks .
Lester Burnham
Rancher
Joined: Oct 14, 2008
Posts: 1337
posted
0
Thanks Burnham for the response .
The name is Lester. If you prefer to address me by last name, then the proper way to do that is to prefix it by "Mr." First name is fine, though.
When i used AJAX and used xmlhttprequest.responseText , i could get the value without using eval also
Not if the value is a JavaScript expression.
Or please share any such scenario
Evaluating JSON retrieved through an AJAX call is a major use case. Evaluating user input locally in the browser (without a server round-trip) another.
This is a good place to mention that using eval is much slower than without using eval. If you can execute your expression without eval, it is better to do so.