The JavaScript eval() is used to execute an argument. The code gets execute slower when the eval() method is used. It also has security implementations since it has a different scope of execution.
eval()
is a function property of the global object.
The argument of the eval()
function is a string. If the string represents an expression, eval()
evaluates the expression. If the argument represents one or more JavaScript statements, eval()
evaluates the statements. Do not call eval()
to evaluate an arithmetic expression; JavaScript evaluates arithmetic expressions automatically.
Example
Here’s how you can implement eval() function −
<html>
<body>
<script>
var a = 30;
var b = 12;
var res1 = eval("a * b") + "<br>";
var res2 = eval("5 + 10") + "<br>";
document.write(res1);
document.write(res2);
</script>
</body>
</html>