File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Why to use eval function inside Javascript

 
Ravi Kiran Va
Ranch Hand
Posts: 2234
Eclipse IDE Firefox Browser Redhat
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ,

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 .
 
Lester Burnham
Rancher
Posts: 1337
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ravi Kiran Va
Ranch Hand
Posts: 2234
Eclipse IDE Firefox Browser Redhat
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Burnham for the response .

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
Posts: 1337
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ravi Kiran Va
Ranch Hand
Posts: 2234
Eclipse IDE Firefox Browser Redhat
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks .
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 33671
316
Eclipse IDE Java VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Sriram Ramani
Greenhorn
Posts: 4
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do not use eval , its very costly and it is recommended to avoid it in case of the security measures.

In case of JSON use JSon.parse(string);

in case of the local browser evaluations use the [] or the . (dot) notation

Reference http://www.javascripttoolbox.com/bestpractices/#eval

Enjoy Javascript
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic