JavaScript eval() Function

Beginner
7 min read
Updated: Nov 2026
3 examples
Global function

Definition

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);
CallWhat 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

ParameterDescription
codeA 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 ValueDescription
The result of the codeThe 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 itselfWhen 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));
Try It Yourself

How It Works

  1. The text is code. eval("2 + 3 * 4") runs the maths with the normal rules, so multiplication goes first and the answer is 14.
  2. Any expression works. Function calls, string joins and array expressions all run and return their value.
  3. 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.
  4. 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.

JavaScript
globalThis.level = "global";

function check() {
  const level = "local";

  console.log("Direct eval: " + eval("level"));
  console.log("Indirect eval: " + (0, eval)("level"));
}

check();
Try It Yourself

How It Works

  1. Two variables with one name. One level is global, the other is local inside check().
  2. Direct call. eval("level") is written exactly as eval(...), so it runs inside check() and finds the local level.
  3. 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.
  4. 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().

JavaScript
try {
  eval("2 +");
} catch (err) {
  console.log("Invalid code: " + err.name);
}

try {
  eval("missingName + 1");
} catch (err) {
  console.log("Unknown name: " + err.name);
}

const text = '{"name": "Mia", "age": 28}';
const user = JSON.parse(text);
console.log("JSON.parse: " + user.name + ", " + user.age);

const operations = {
  add: (a, b) => a + b,
  multiply: (a, b) => a * b
};
const choice = "multiply";
console.log("Safe lookup: " + operations[choice](6, 7));
Try It Yourself

How It Works

  1. Invalid code. "2 +" is not complete JavaScript, so eval() throws a SyntaxError. The catch block prints its name.
  2. Errors inside the code. missingName does not exist, so the code throws a ReferenceError through eval().
  3. Data: JSON.parse(). Text that holds data should go through JSON.parse(). It reads only data and can never run code.
  4. 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.

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.

ES1 Standard since
Google Chrome All versions · Desktop & Mobile
Full support
Microsoft Edge All versions · Chromium & Legacy
Full support
Mozilla Firefox All versions · Desktop & Mobile
Full support
Apple Safari All versions · macOS & iOS
Full support
Opera All versions
Full support
Samsung Internet All versions · Android
Full support
Android WebView All versions
Full support
Node.js All versions · Server runtime
Supported
Deno All versions · JavaScript runtime
Supported
Bun All versions · JavaScript runtime
Supported
Internet Explorer All 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.

Next: isFinite()

Learn how to check that a value is a real number and not NaN or Infinity.

isFinite() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

16 people found this page helpful