JavaScript Reference ยท Global Functions and Values
The eval() function
Reference for JavaScript eval(): how it runs code from a string, direct versus indirect calls, strict mode, security risks and safer alternatives.
The global eval() function takes a string, parses it as a JavaScript script and runs it. The value of the last expression statement in that script becomes the return value of eval(). If the argument is not a string, eval() returns it unchanged.
eval() is almost never the right tool. It runs with the full power of your page, so feeding it text that came from a user, a URL or a server is a classic injection hole. It also blocks many engine optimizations and makes code hard to read and debug. Use it only when you truly must execute source text, and prefer the alternatives listed below.
const result = eval("2 + 3 * 4");
Try it
Press Run (or Ctrl + Enter) to see the console output here.
No output.
Press Run (or Ctrl + Enter) to see the console output here.
No output.
Syntax
eval(script)
| Name | Description |
|---|---|
script |
A string containing a valid JavaScript script. Any other type is returned unchanged without being evaluated. |
Return value: the completion value of the script, which is the value of the last statement that produced one. It is undefined when the script produces no value, for example a lone var declaration.
Exceptions: a SyntaxError if the string cannot be parsed, and any error thrown by the evaluated code itself.
Usage notes
- Direct versus indirect. A call written exactly as
eval(...)is a direct eval: it runs in the current scope and can read and write local variables. Calling it any other way (const e = eval; e("..."),(0, eval)("..."),globalThis.eval("...")) is an indirect eval: it runs in the global scope. - Declarations. In sloppy mode a direct eval can create new
varand function declarations in the surrounding function scope. In strict mode, and in an indirect eval,varand function declarations stay inside the evaluated code.let,constandclassdeclarations are always local to the eval. - Security. Never pass untrusted text to
eval(). A single string such as one that reads cookies or sends data elsewhere runs with your page's privileges. A strict Content Security Policy disableseval()entirely (the call throws an error unless'unsafe-eval'is allowed), which is one more reason to avoid it. - Performance and tooling. Engines cannot predict what the code will touch, so they optimize functions that call direct
eval()less aggressively. Minifiers cannot rename variables the string might reference. Debuggers and linters see only a string. - JSON is not code. Do not use
eval()to parse JSON; useJSON.parse(), which is safer and faster and rejects anything that is not data. - Safer alternatives. Look up properties with bracket notation (
obj[name]), pick functions from an object orMap, parse data withJSON.parse(), and write small parsers or a lookup table for expressions. If you need to compile a function from text,new Function()is slightly safer because it only sees global scope, but it carries the same injection risk. - Not a calculator.
eval("1 + 1")works, but so does anything else a user types. Parse and validate input instead.
Browser support
Supported in every browser, but it is commonly blocked by Content Security Policy on production sites, so code that relies on it may fail there.
Related
JSON.parse(),let,var- Lessons: Strict mode, Security, Scope