Tag Archives: Using eval() method in Java Script

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.

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.

R Vashi