eval function allows to execute a string of code.
The syntax is:
let result = eval(code);
A string of code may be long, contain line breaks, function declarations, variables and so on.
The result of
eval is the result of the last statement.
The eval’ed code is executed in the current lexical environment, so it can see outer variables:
It can change outer variables as well:
In strict mode,
eval has its own lexical environment. So functions and variables, declared inside eval, are not visible outside:
eval doesn’t have its own lexical environment, so we would see
In modern programming
eval is used very sparingly. It’s often said that “eval is evil”.
eval. But that time passed a decade ago.
Right now, there’s almost no reason to use
Please note that its ability to access outer variables has side-effects.
Code minifiers (tools used before JS gets to production, to compress it) rename local variables into shorter ones (like
b etc) to make the code smaller. That’s usually safe, but not if
eval is used, as local variables may be accessed from eval’ed code string. So minifiers don’t do that renaming for all variables potentially visible from
eval. That negatively affects code compression ratio.
Using outer local variables inside
eval is also considered a bad programming practice, as it makes maintaining the code more difficult.
There are two ways how to be totally safe from such problems.
If eval’ed code doesn’t use outer variables, please call
This way the code is executed in the global scope:
If eval’ed code needs local variables, change
new Function and pass them as arguments:
new Function construct is explained in the chapter The "new Function" syntax. It creates a function from a string, also in the global scope. So it can’t see local variables. But it’s so much clearer to pass them explicitly as arguments, like in the example above.
A call to
eval(code) runs the string of code and returns the result of the last statement.
- Can access outer local variables. That’s considered bad practice.
- Instead, to
evalthe code in the global scope, use
- Or, if your code needs some data from the outer scope, use
new Functionand pass it as arguments.