Using eval() method in Java Script

The eval() method accepts a string of JavaScript statements(method, expressions)  and evaluates it as JavaScript source code.
eval() returns the value of the executed JavaScript statement.

WE mainly use eval() method while working with AJAX response which sometime contains some executable java script code to run.
Even many of the Java script lib/framework uses this heavily in their AJAX frameworks.

Syntax:
1. eval(statement) //statement can be any java script method.

2. eval(string) //any raw string containing the java script expressions. e.g eval(“alert(‘hello’)”);
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 the same way it execute the other methods defined in script tag.

[NOTE] This method is widely supported by most of the known browsers like IE, Mozilla, OPera, Safari, Chrome.

Hope this helps.


Thanks
R Vashi

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s