• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
Sheriffs:
Saloon Keepers:
  • Peter Rooke
Bartenders:

Why to use eval function inside Javascript

 
Ranch Hand
Posts: 2234
Eclipse IDE Firefox Browser Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 .
 
Rancher
Posts: 1337
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks .
 
author & internet detective
Posts: 42271
951
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
reply
    Bookmark Topic Watch Topic
  • New Topic