aspose file tools*
The moose likes HTML, CSS and JavaScript and the fly likes Why to use eval function inside Javascript Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "Why to use eval function inside Javascript " Watch "Why to use eval function inside Javascript " New topic
Author

Why to use eval function inside Javascript

Ravi Kiran Va
Ranch Hand

Joined: Apr 18, 2009
Posts: 2234

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 .

Save India From Corruption - Anna Hazare.
Lester Burnham
Rancher

Joined: Oct 14, 2008
Posts: 1337
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

Joined: Apr 18, 2009
Posts: 2234

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

Joined: Oct 14, 2008
Posts: 1337
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

Joined: Apr 18, 2009
Posts: 2234

Thanks .
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 29287
    
140

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.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Sriram Ramani
Greenhorn

Joined: Feb 28, 2011
Posts: 4
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Why to use eval function inside Javascript
 
Similar Threads
how to call javascript function in the previous html file
Execute the Code
display text string with Javascript on screen
Validating Parser
Javascript function isn't returning a value