eval() takes text that contains JavaScript code, runs it, and returns the value of the last expression. eval("2 + 3 * 4") returns 14.
It is powerful, and for that reason it is dangerous. If the text comes from a user, a URL or a server, someone can run their own code on your page. It also makes code slower and harder to read. Learn it so you can recognise it, and prefer safer tools such as JSON.parse().
Remember
eval(code);
code -> a string of JavaScript code
returns -> the value of the last expression that ran
(or undefined for statements)
If code is not a string, eval returns it unchanged.
Never run eval on text from a user.
Syntax
Call eval() by name and give it a string of code. A call written exactly as eval(...) is a direct call and can see the variables around it.
JavaScript
eval(code);
Call
What it does
eval("2 + 3 * 4")
Runs the maths in the text. Result: 14.
eval("Math.max(4, 9, 2)")
Runs a function call. Result: 9.
eval(42)
The value is not a string, so it is returned unchanged. Result: 42.
eval("2 +")
The code is not valid. It throws a SyntaxError.
Built into JavaScript (ES1). It is a global function, so you call it by name with no import. It works, but it is discouraged in almost every case. See also: parseInt(), isNaN() and encodeURIComponent().
Library declarations
JavaScript
// Global function (ES1)
eval(code)
// returns the value of the last expression, or the value itself if it is not a string
// throws SyntaxError for invalid code, or any error thrown by the code
Parameters
Parameter
Description
code
A string of JavaScript code. It can hold one expression or many statements. If you pass a value that is not a string (a number, an object, undefined), eval() returns it as it is and runs nothing.
Return Value
Return Value
Description
The result of the code
The value of the last expression that ran, such as 14 for "2 + 3 * 4". Statements that give no value, like "var a = 1", return undefined.
The value itself
When the argument is not a string, it is returned unchanged.
SyntaxError (thrown)
The text is not valid JavaScript. Any error thrown by the code itself (such as a ReferenceError) is thrown by eval() too.
JavaScript Examples
Example 1 — Run Code Written as Text
Maths, function calls and several statements in one string, run by eval().
JavaScript
console.log(eval("2 + 3 * 4"));
console.log(eval("Math.max(4, 9, 2)"));
console.log(eval("'Code' + 'ToFun'"));
console.log(eval("[1, 2, 3].length"));
console.log(eval("let a = 5; let b = 6; a * b"));
console.log(eval(42));
The text is code.eval("2 + 3 * 4") runs the maths with the normal rules, so multiplication goes first and the answer is 14.
Any expression works. Function calls, string joins and array expressions all run and return their value.
Several statements. The result of "let a = 5; let b = 6; a * b" is the value of the last expression, 30. The two let variables stay inside the eval() call.
Not a string.eval(42) has no code to run, so it returns 42 as it is.
Example 2 — Direct and Indirect eval()
A direct call can see the local variables. An indirect call runs in the global scope only.
Two variables with one name. One level is global, the other is local inside check().
Direct call.eval("level") is written exactly as eval(...), so it runs inside check() and finds the local level.
Indirect call.(0, eval)("level") calls the same function without the plain name, so it runs in the global scope and finds the global level.
Why it matters. A direct eval() can read and change your local variables, which is one reason it is hard to optimise and easy to misuse. Prefer the indirect form if you ever must run code.
Example 3 — Handle Errors and Use Safer Choices
Bad code throws an error you can catch. For data and for choices there are better tools than eval().
Invalid code."2 +" is not complete JavaScript, so eval() throws a SyntaxError. The catch block prints its name.
Errors inside the code.missingName does not exist, so the code throws a ReferenceError through eval().
Data: JSON.parse(). Text that holds data should go through JSON.parse(). It reads only data and can never run code.
Choices: an object. Instead of building code like "6 " + operator + " 7", keep the allowed functions in an object and look one up by name. Nothing outside your list can run.
Compatibility
Browser & Runtime Support
eval() exists in every browser and every Node.js version. But a page that sets a strict Content Security Policy (without unsafe-eval) blocks it, and it is not allowed in some extension and app environments.
✓ ES1 · Works everywhere
eval()
Supported in all browsers and in Node.js, Deno and Bun, unless a security policy blocks it.
ES1Standard since
Google ChromeAll versions · Desktop & Mobile
Full support
Microsoft EdgeAll versions · Chromium & Legacy
Full support
Mozilla FirefoxAll versions · Desktop & Mobile
Full support
Apple SafariAll versions · macOS & iOS
Full support
OperaAll versions
Full support
Samsung InternetAll versions · Android
Full support
Android WebViewAll versions
Full support
Node.jsAll versions · Server runtime
Supported
DenoAll versions · JavaScript runtime
Supported
BunAll versions · JavaScript runtime
Supported
Internet ExplorerAll versions
Full support
eval()Works, but avoid
Bottom line: It runs everywhere unless the page blocks it with a Content Security Policy. Treat it as a last resort.
Frequently Asked Questions
It runs JavaScript code that is written as a string and returns the value of the last expression. For example eval("2 + 3 * 4") returns 14. If you pass something that is not a string, it is returned unchanged.
If any part of the text comes from a user, a URL, a form or a server, an attacker can insert their own code, and it runs with the full power of your page. That is called code injection. eval() is also slower, because the engine cannot optimise code it has not seen yet, and it makes errors harder to find.
For data, use JSON.parse(). For a choice of actions, keep the allowed functions in an object and look one up by name. To read a property by name, use obj[name]. For a calculator, write a small parser or use a maths library. You almost never need eval().
A direct call, written exactly as eval(code), can see and change the local variables around it. An indirect call, such as (0, eval)(code) or const e = eval; e(code), always runs in the global scope and cannot see local variables.
new Function("a", "return a * 2") also builds code from text, but the code runs in the global scope and cannot see local variables. It has the same security risk, so the same rule applies: never run text you do not trust.