eval()
The global eval() function parses a string as JavaScript code, runs it, and returns the value of the last expression or statement it evaluated. The code runs with the same privileges as the code that called it:
> eval("2 + 3 * 4")
14
> eval("const items = [4, 8]; items.length * 10")
20
eval() is an ECMAScript built-in, so browsers and server runtimes both provide it, although a host can refuse to run the code. A call written as eval(...) is a direct eval and sees the caller’s local variables. Any other call, such as through an alias, (0, eval)(...), or eval?.(...), is an indirect eval that runs the code in global scope.
Syntax
The eval() function takes one argument:
eval(<script>)
| Parameter | Description |
|---|---|
script |
A string holding a JavaScript expression, statement, or sequence of statements. It’s parsed as a script, not a module, so import declarations aren’t allowed |
The function isn’t a constructor, so calling it with new throws a TypeError.
Return Value
The result is the completion value of the evaluated code, or undefined when the code produces no value, such as a lone declaration. A value that isn’t a string primitive comes back unchanged without being run. In browsers that support Trusted Types, a TrustedScript object is the one exception, and its code runs.
The call throws a SyntaxError when the string isn’t valid script code. Any error the code throws while it runs propagates to the caller.
In browsers, a Content Security Policy can block eval() entirely. When a page’s policy sets script-src or default-src without allowing 'unsafe-eval', the browser refuses to run the string.
Examples
A spreadsheet-style tool stores a formula as text and needs its result. A direct call can read the local variables that the formula names:
> function calculate(formula) {
… const price = 25;
… const quantity = 4;
… return eval(formula);
… }
undefined
> calculate("price * quantity")
100
An indirect call runs the same text in global scope, so the local price binding is out of reach:
> function calculateGlobally(formula) {
… const price = 25;
… return (0, eval)(formula);
… }
undefined
> calculateGlobally("typeof price")
'undefined'
> calculateGlobally("typeof Math")
'object'
Switch between the two call forms and pick a formula to see which scopes the evaluated string can read and what each call returns:
A var declaration inside a direct, non-strict eval lands in the caller’s scope. A let or const declaration stays inside the evaluated code:
> function defineSettings() {
… eval("var retries = 3; let timeout = 500;");
… return [typeof retries, typeof timeout];
… }
undefined
> defineSettings()
[ 'number', 'undefined' ]
In strict mode, which includes every ES module, the var declaration stays inside the eval too.
Common Gotchas
A string that looks like an object literal parses as a block statement with a label, so the call returns the last value inside the braces. Wrapping the text in parentheses makes it an expression, and JSON.parse() reads data without running code:
> eval("{ retries: 3 }")
3
> eval("({ retries: 3 })")
{ retries: 3 }
> JSON.parse('{ "retries": 3 }')
{ retries: 3 }
A String object isn’t a string primitive, so eval() returns it as is instead of running it:
> eval(new String("2 + 2"))
[String: '2 + 2']
> eval(String(new String("2 + 2")))
4
Building code from outside input lets that input run as code. A sort field read from a URL can carry an extra expression after a comma, and the evaluated string then runs it:
> const order = { price: 25, quantity: 4 };
undefined
> const field = "price, globalThis.hacked = true";
undefined
> eval("order." + field)
true
> globalThis.hacked
true
> order[field]
undefined
Bracket notation reads a property whose name is in a variable without parsing any code, so the same input only looks up a property that doesn’t exist.
Related Resources
Course
JavaScript Scope, Closures, and Functional Patterns for Pythonistas
Port a bouldering gym's front-desk script to JavaScript, and learn where names live and when they exist, how closures keep state, and how to work with rest parameters, callbacks, and timers.
Have a question about this? Mentor AI can show you examples, compare related terms, and point you to tutorials.
By Leodanis Pozo Ramos • Updated Nov. 15, 2026