--- title: eval() slug: Web/JavaScript/Reference/Global_Objects/eval page-type: javascript-function browser-compat: javascript.builtins.eval sidebar: jssidebar --- > [!WARNING] > The argument passed to this method is dynamically evaluated and executed as JavaScript. > APIs like this are known as [injection sinks](/en-US/docs/Web/API/Trusted_Types_API#concepts_and_usage), and are potentially a vector for [cross-site-scripting (XSS)](/en-US/docs/Web/Security/Attacks/XSS) attacks. > > You can mitigate this risk by always passing {{domxref("TrustedScript")}} objects instead of strings and [enforcing trusted types](/en-US/docs/Web/API/Trusted_Types_API#using_a_csp_to_enforce_trusted_types). > > See [Security considerations](#security_considerations) for more information. The **`eval()`** function evaluates JavaScript code represented as a string and returns its completion value. The source is parsed as a script. {{InteractiveExample("JavaScript Demo: eval()")}} ```js interactive-example console.log(eval("2 + 2")); // Expected output: 4 console.log(eval(new String("2 + 2"))); // Expected output: 2 + 2 console.log(eval("2 + 2") === eval("4")); // Expected output: true console.log(eval("2 + 2") === eval(new String("2 + 2"))); // Expected output: false ``` ## Syntax ```js-nolint eval(script) ``` ### Parameters - `script` - : A {{domxref("TrustedScript")}} instance or string representing a JavaScript expression, statement, or sequence of statements. The expression can include variables and properties of existing objects. It will be parsed as a script, so [`import`](/en-US/docs/Web/JavaScript/Reference/Statements/import) declarations (which can only exist in modules) are not allowed. ### Return value The completion value of evaluating the given code. If the completion value is empty, {{jsxref("undefined")}} is returned. If `script` is not a {{domxref("TrustedScript")}} or string primitive, `eval()` returns the argument unchanged. ### Exceptions - {{jsxref("SyntaxError")}} - : The `script` parameter cannot be parsed as a script. - {{jsxref("TypeError")}} - : `script` is a string when [Trusted Types](/en-US/docs/Web/API/Trusted_Types_API) are [enforced by a CSP](/en-US/docs/Web/API/Trusted_Types_API#using_a_csp_to_enforce_trusted_types) and no default policy is defined. The method also throws any exception that occurs during evaluation of the code. ## Description `eval()` is a function property of the global object. The argument of the `eval()` 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 of [`let`](/en-US/docs/Web/JavaScript/Reference/Statements/let) is undefined), so it's recommended to not rely on statements' completion values. In strict mode, declaring a variable named `eval` or re-assigning `eval` is a {{jsxref("SyntaxError")}}. ```js-nolint example-bad "use strict"; const eval = 1; // SyntaxError: Unexpected eval or arguments in strict mode ``` If the argument of `eval()` is not a {{domxref("TrustedScript")}} or string, `eval()` returns the argument unchanged. In the following example, passing a `String` object instead of a primitive causes `eval()` to return the `String` object rather than evaluating the string. ```js 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 can [coerce the argument to a string](/en-US/docs/Web/JavaScript/Reference/Global_Objects/String#string_coercion) yourself before passing it to `eval()`. ```js const expression = new String("2 + 2"); eval(String(expression)); // returns 4 ``` ### Direct and indirect eval There are two modes of `eval()` calls: _direct_ eval and _indirect_ eval. Direct eval, as the name implies, refers to _directly_ calling the global `eval` function with `eval(...)`. Everything else, including invoking it via an aliased variable, via a member access or other expression, or through the optional chaining [`?.`](/en-US/docs/Web/JavaScript/Reference/Operators/Optional_chaining) operator, is indirect. ```js // Direct call eval("x + y"); // Indirect call using the comma operator to return eval (0, eval)("x + y"); // Indirect call through optional chaining eval?.("x + y"); // Indirect call using a variable to store and return eval const myEval = eval; myEval("x + y"); // Indirect call through member access const obj = { eval }; obj.eval("x + y"); ``` Indirect eval can be seen as if the code is evaluated within a separate `