JavaScript eval() function

Shirshendu Bhowmick
3 min readJun 29, 2018

--

Today we will look into JavaScript eval() function.

TL;DR JavaScript eval function is capable of executing JavaScript code passed as a string to it. The eval is a part of the JavaScript global object. The return value of eval() is the value of last expression evaluated, if it is empty then it will return undefined. Example:

eval("console.log('Hello World')");

The above code will result a “Hello World” in the console.

FYI eval() is also capable of executing multiple JavaScript statements.

eval("var x = 10; console.log(x)");

The above code will result a “10” in the console.

But wait there is more to eval() than you think.

In the above examples eval() works in global scope having access to global scope. What happens if you call eval() inside a function ? Lets see.

var x = 10
function foo () {
var y = 20
eval ('var z = x + y');
console.log(z);
}
foo(); // will result 30

This seems to be pretty normal. as eval() inside of function foo it has access to both global scope and local scope. Note that as variable z and y are created inside function foo their scope is limited to this function only.

Now comes the twist indirect call to eval(), consider the below example:

var x = 10
function foo () {
var y = 20
var indirectEval = eval;
indirectEval ('var z = x + y');
console.log(z);
}
foo(); // will result a reference error y

This is because this kind calling of eval is actually calling it from the global scope and it has no access to the current local scope i.e. foo that’s why we are getting a reference error for y.

Now comes use strict with eval(). Let us first check this example:

eval("var x = 10");
console.log(x); // will result 10

This is simple and normal, now lets run the same code in strict mode:

'use strict'
eval("var x = 10");
console.log(x); // will result a reference error

In the first example i.e. with out using the strict mode, eval executes a JS statement and adds the variables to the global scope. While in strict mode it will not do so. The variables will be limited inside of eval() only.

Lets check one more example:

eval("( function test () {console.log('hello')} )");

What do you think this will do ? This will create a function test and add it to the global object ? No it will just evaluate the expression and thus it will return the function test that’s it. Also not that “(“ & “)” before & after function definition.

eval("function test () {console.log('hello')}");

Unlike the previous example it will return undefined. So “(“ & “)” are important in case defining functions inside eval().

Beware!

Using eval() sometimes maybe extremely dangerous, it depends upon the programmer how he / she is using it. For example you are taking a mathematical expression as user input and you want to evaluate that, so you thought of using eval(). Instead of inputting a mathematical expression if the user inputs a malicious JavaScript code and eval() executes that code, the result might be extremely dangerous.

That’s it for this article.

For more information on eval() please refer to MDN link

Thank You.

--

--

Shirshendu Bhowmick
Shirshendu Bhowmick

Responses (2)