JavaScript eval()

Examples

Execute JavaScript code with eval():

let x = 10;
let y = 20;
let text = "x * y";
let result = eval(text);
Try it Yourself »

Never use eval(). Use code or a function instead:

let x = 10;
let y = 20;
let result = x * y;
Try it Yourself »

Description

The eval() method evaluates or executes an argument.

If the argument is an expression, eval() evaluates the expression. If the argument is one or more JavaScript statements, eval() executes the statements.

Do NOT use eval()

Executing JavaScript from a string is an BIG security risk.

With eval(), malicious code can run inside your application without permission.

With eval(), third-party code can see the scope of your application, which can lead to possible attacks.


Syntax

eval(string)

Parameters

Parameter Description
string A JavaScript expression, variable, statement, or sequence of statements

Browser Support

eval() is an ECMAScript1 (ES1) feature.

ES1 (JavaScript 1997) is fully supported in all browsers:

Chrome Edge Firefox Safari Opera IE
Yes Yes Yes Yes Yes Yes


Copyright 1999-2023 by Refsnes Data. All Rights Reserved.