eval()
BaselineWidely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.
Warning:Executing JavaScript from a string is an enormous security risk. It is far too easy for a bad actor to run arbitrary code when you useeval()
. SeeNever use direct eval()!, below.
Theeval()
function evaluates JavaScript code represented as a string and returns its completion value. The source is parsed as a script.
Try it
console.log(eval("2 + 2"));// Expected output: 4console.log(eval(new String("2 + 2")));// Expected output: 2 + 2console.log(eval("2 + 2") === eval("4"));// Expected output: trueconsole.log(eval("2 + 2") === eval(new String("2 + 2")));// Expected output: false
Syntax
eval(script)
Parameters
Return value
The completion value of evaluating the given code. If the completion value is empty,undefined
is returned. Ifscript
is not a string primitive,eval()
returns the argument unchanged.
Exceptions
Throws any exception that occurs during evaluation of the code, includingSyntaxError
ifscript
fails to be parsed as a script.
Description
eval()
is a function property of the global object.
The argument of theeval()
function is a string. It will evaluate the source string as a script body, which means both statements and expressions are allowed. It returns the completion value of the code. For expressions, it's the value the expression evaluates to. Many statements and declarations have completion values as well, but the result may be surprising (for example, the completion value of an assignment is the assigned value, but the completion value oflet
is undefined), so it's recommended to not rely on statements' completion values.
In strict mode, declaring a variable namedeval
or re-assigningeval
is aSyntaxError
.
"use strict";const eval = 1; // SyntaxError: Unexpected eval or arguments in strict mode
If the argument ofeval()
is not a string,eval()
returns the argument unchanged. In the following example, passing aString
object instead of a primitive causeseval()
to return theString
object rather than evaluating the string.
eval(new String("2 + 2")); // returns a String object containing "2 + 2"eval("2 + 2"); // returns 4
To work around the issue in a generic fashion, you cancoerce the argument to a string yourself before passing it toeval()
.
const expression = new String("2 + 2");eval(String(expression)); // returns 4
Direct and indirect eval
There are two modes ofeval()
calls:direct eval andindirect eval. Direct eval, as the name implies, refers todirectly calling the globaleval
function witheval(...)
. Everything else, including invoking it via an aliased variable, via a member access or other expression, or through the optional chaining?.
operator, is indirect.
// Direct calleval("x + y");// Indirect call using the comma operator to return eval(0, eval)("x + y");// Indirect call through optional chainingeval?.("x + y");// Indirect call using a variable to store and return evalconst myEval = eval;myEval("x + y");// Indirect call through member accessconst obj = { eval };obj.eval("x + y");
Indirect eval can be seen as if the code is evaluated within a separate<script>
tag. This means:
Indirect eval works in the global scope rather than the local scope, and the code being evaluated doesn't have access to local variables within the scope where it's being called.
jsfunction test() { const x = 2; const y = 4; // Direct call, uses local scope console.log(eval("x + y")); // Result is 6 // Indirect call, uses global scope console.log(eval?.("x + y")); // Throws because x is not defined in global scope}
Indirect
eval
does not inherit the strictness of the surrounding context, and is only instrict mode if the source string itself has a"use strict"
directive.jsfunction nonStrictContext() { eval?.(`with (Math) console.log(PI);`);}function strictContext() { "use strict"; eval?.(`with (Math) console.log(PI);`);}function strictContextStrictEval() { "use strict"; eval?.(`"use strict"; with (Math) console.log(PI);`);}nonStrictContext(); // Logs 3.141592653589793strictContext(); // Logs 3.141592653589793strictContextStrictEval(); // Uncaught SyntaxError: Strict mode code may not include a with statement
On the other hand, direct eval inherits the strictness of the invoking context.
jsfunction nonStrictContext() { eval(`with (Math) console.log(PI);`);}function strictContext() { "use strict"; eval(`with (Math) console.log(PI);`);}function strictContextStrictEval() { "use strict"; eval(`"use strict"; with (Math) console.log(PI);`);}nonStrictContext(); // Logs 3.141592653589793strictContext(); // Uncaught SyntaxError: Strict mode code may not include a with statementstrictContextStrictEval(); // Uncaught SyntaxError: Strict mode code may not include a with statement
var
-declared variables andfunction declarations would go into the surrounding scope if the source string is not interpreted in strict mode — for indirect eval, they become global variables. If it's a direct eval in a strict mode context, or if theeval
source string itself is in strict mode, thenvar
and function declarations do not "leak" into the surrounding scope.js// Neither context nor source string is strict,// so var creates a variable in the surrounding scopeeval("var a = 1;");console.log(a); // 1// Context is not strict, but eval source is strict,// so b is scoped to the evaluated scripteval("'use strict'; var b = 1;");console.log(b); // ReferenceError: b is not definedfunction strictContext() { "use strict"; // Context is strict, but this is indirect and the source // string is not strict, so c is still global eval?.("var c = 1;"); // Direct eval in a strict context, so d is scoped eval("var d = 1;");}strictContext();console.log(c); // 1console.log(d); // ReferenceError: d is not defined
let
andconst
declarations within the evaluated string are always scoped to that script.Direct eval may have access to additional contextual expressions. For example, in a function's body, one can use
new.target
:jsfunction Ctor() { eval("console.log(new.target)");}new Ctor(); // [Function: Ctor]
Never use direct eval()!
Using directeval()
suffers from multiple problems:
eval()
executes the code it's passed with the privileges of the caller. If you runeval()
with a string that could be affected by a malicious party, you may end up running malicious code on the user's machine with the permissions of your webpage / extension. More importantly, allowing third-party code to access the scope in whicheval()
was invoked (if it's a direct eval) can lead to possible attacks that reads or changes local variables.eval()
is slower than the alternatives, since it has to invoke the JavaScript interpreter, while many other constructs are optimized by modern JS engines.- Modern JavaScript interpreters convert JavaScript to machine code. This means that any concept of variable naming gets obliterated. Thus, any use of
eval()
will force the browser to do long expensive variable name lookups to figure out where the variable exists in the machine code and set its value. Additionally, new things can be introduced to that variable througheval()
, such as changing the type of that variable, forcing the browser to re-evaluate all of the generated machine code to compensate. - Minifiers give up on any minification if the scope is transitively depended on by
eval()
, because otherwiseeval()
cannot read the correct variable at runtime.
There are many cases where the use ofeval()
or related methods can be optimized or avoided altogether.
Using indirect eval()
Consider this code:
function looseJsonParse(obj) { return eval(`(${obj})`);}console.log(looseJsonParse("{ a: 4 - 1, b: function () {}, c: new Map() }"));
Simply using indirect eval and forcing strict mode can make the code much better:
function looseJsonParse(obj) { return eval?.(`"use strict";(${obj})`);}console.log(looseJsonParse("{ a: 4 - 1, b: function () {}, c: new Map() }"));
The two code snippets above may seem to work the same way, but they do not; the first one using direct eval suffers from multiple problems.
It is a great deal slower, due to more scope inspections. Notice
c: new Map()
in the evaluated string. In the indirect eval version, the object is being evaluated in the global scope, so it is safe for the interpreter to assume thatMap
refers to the globalMap()
constructor instead of a local variable calledMap
. However, in the code using direct eval, the interpreter cannot assume this. For example, in the following code,Map
in the evaluated string doesn't refer towindow.Map()
.jsfunction looseJsonParse(obj) { class Map {} return eval(`(${obj})`);}console.log(looseJsonParse(`{ a: 4 - 1, b: function () {}, c: new Map() }`));
Thus, in the
eval()
version of the code, the browser is forced to make the expensive lookup call to check to see if there are any local variables calledMap()
.If not using strict mode,
var
declarations within theeval()
source becomes variables in the surrounding scope. This leads to hard-to-debug issues if the string is acquired from external input, especially if there's an existing variable with the same name.Direct eval can read and mutate bindings in the surrounding scope, which may lead to external input corrupting local data.
When using direct
eval
, especially when the eval source cannot be proven to be in strict mode, the engine — and build tools — have to disable all optimizations related to inlining, because theeval()
source can depend on any variable name in its surrounding scope.
However, using indirecteval()
does not allow passing extra bindings other than existing global variables for the evaluated source to read. If you need to specify additional variables that the evaluated source should have access to, consider using theFunction()
constructor.
Using the Function() constructor
TheFunction()
constructor is very similar to the indirect eval example above: it also evaluates the JavaScript source passed to it in the global scope without reading or mutating any local bindings, and therefore allows engines to do more optimizations than directeval()
.
The difference betweeneval()
andFunction()
is that the source string passed toFunction()
is parsed as a function body, not as a script. There are a few nuances — for example, you can usereturn
statements at the top level of a function body, but not in a script.
TheFunction()
constructor is useful if you wish to create local bindings within your eval source, by passing the variables as parameter bindings.
function add(a, b) { return a + b;}function runCodeWithAddFunction(obj) { return Function("add", `"use strict";return (${obj});`)(add);}console.log(runCodeWithAddFunction("add(5, 7)")); // 12
Botheval()
andFunction()
implicitly evaluate arbitrary code, and are forbidden in strictCSP settings. There are also additional safer (and faster!) alternatives toeval()
orFunction()
for common use-cases.
Using bracket accessors
You should not useeval()
to access properties dynamically. Consider the following example where the property of the object to be accessed is not known until the code is executed. This can be done witheval()
:
const obj = { a: 20, b: 30 };const propName = getPropName(); // returns "a" or "b"const result = eval(`obj.${propName}`);
However,eval()
is not necessary here — in fact, it's more error-prone, because ifpropName
is not a valid identifier, it leads to a syntax error. Moreover, ifgetPropName
is not a function you control, this may lead to execution of arbitrary code. Instead, use theproperty accessors, which are much faster and safer:
const obj = { a: 20, b: 30 };const propName = getPropName(); // returns "a" or "b"const result = obj[propName]; // obj["a"] is the same as obj.a
You can even use this method to access descendant properties. Usingeval()
, this would look like:
const obj = { a: { b: { c: 0 } } };const propPath = getPropPath(); // suppose it returns "a.b.c"const result = eval(`obj.${propPath}`); // 0
Avoidingeval()
here could be done by splitting the property path and looping through the different properties:
function getDescendantProp(obj, desc) { const arr = desc.split("."); while (arr.length) { obj = obj[arr.shift()]; } return obj;}const obj = { a: { b: { c: 0 } } };const propPath = getPropPath(); // suppose it returns "a.b.c"const result = getDescendantProp(obj, propPath); // 0
Setting a property that way works similarly:
function setDescendantProp(obj, desc, value) { const arr = desc.split("."); while (arr.length > 1) { obj = obj[arr.shift()]; } return (obj[arr[0]] = value);}const obj = { a: { b: { c: 0 } } };const propPath = getPropPath(); // suppose it returns "a.b.c"const result = setDescendantProp(obj, propPath, 1); // obj.a.b.c is now 1
However, beware that using bracket accessors with unconstrained input is not safe either — it may lead toobject injection attacks.
Using callbacks
JavaScript hasfirst-class functions, which means you can pass functions as arguments to other APIs, store them in variables and objects' properties, and so on. Many DOM APIs are designed with this in mind, so you can (and should) write:
// Instead of setTimeout("…", 1000) use:setTimeout(() => { // …}, 1000);// Instead of elt.setAttribute("onclick", "…") use:elt.addEventListener("click", () => { // …});
Closures are also helpful as a way to create parameterized functions without concatenating strings.
Using JSON
If the string you're callingeval()
on contains data (for example, an array:"[1, 2, 3]"
), as opposed to code, you should consider switching toJSON, which allows the string to use a subset of JavaScript syntax to represent data.
Note that since JSON syntax is limited compared to JavaScript syntax, many valid JavaScript literals will not parse as JSON. For example, trailing commas are not allowed in JSON, and property names (keys) in object literals must be enclosed in quotes. Be sure to use a JSON serializer to generate strings that will be later parsed as JSON.
Passing carefully constrained data instead of arbitrary code is a good idea in general. For example, an extension designed to scrape contents of web-pages could have the scraping rules defined inXPath instead of JavaScript code.
Examples
Using eval()
In the following code, both of the statements containingeval()
return 42.The first evaluates the string"x + y + 1"
; the second evaluates the string"42"
.
const x = 2;const y = 39;const z = "42";eval("x + y + 1"); // 42eval(z); // 42
eval() returns the completion value of statements
eval()
returns the completion value of statements. Forif
, it would be the last expression or statement evaluated.
const str = "if (a) { 1 + 1 } else { 1 + 2 }";let a = true;let b = eval(str);console.log(`b is: ${b}`); // b is: 2a = false;b = eval(str);console.log(`b is: ${b}`); // b is: 3
The following example useseval()
to evaluate the stringstr
. This string consists of JavaScript statements that assignz
a value of 42 ifx
is five, and assign 0 toz
otherwise. When the second statement is executed,eval()
will cause these statements to be performed, and it will also evaluate the set of statements and return the value that is assigned toz
, because the completion value of an assignment is the assigned value.
const x = 5;const str = `if (x === 5) { console.log("z is 42"); z = 42;} else { z = 0;}`;console.log("z is ", eval(str)); // z is 42 z is 42
If you assign multiple values then the last value is returned.
let x = 5;const str = `if (x === 5) { console.log("z is 42"); z = 42; x = 420;} else { z = 0;}`;console.log("x is", eval(str)); // z is 42 x is 420
eval() as a string defining function requires "(" and ")" as prefix and suffix
// This is a function declarationconst fctStr1 = "function a() {}";// This is a function expressionconst fctStr2 = "(function b() {})";const fct1 = eval(fctStr1); // return undefined, but `a` is available as a global function nowconst fct2 = eval(fctStr2); // return the function `b`
Specifications
Specification |
---|
ECMAScript® 2026 Language Specification # sec-eval-x |