Javascript eval() Function: Complete Guide

JavaScript eval() is a built-in function that evaluates or executes an argument. The eval() function evaluates the expression if an argument is an expression. The eval() is a function property of the global object in JavaScript.

The eval() function accepts a string as an argument, evaluates it as an expression, and returns the result.

Syntax

eval(string)

Arguments

string: It takes a string which is a  JavaScript expression, variable, statement, or sequence of statements.

Return value

The eval() function returns the result of our passed argument. If the result is empty, then it will return undefined.

Example

const a = eval(new String('4 + 6'));
const b = eval('4 + 6');
console.log(a.toString());
console.log(b);

Output

4 + 6
10

We defined two eval functions in the above example and stored them in a variable. In the first eval function, we define a new string as an object so it will print the same as the string, and other eval functions evaluate the passed string and return the total of them.

Empty eval() function in JavaScript

If you pass an empty argument to the eval() function, it will return undefined. Therefore, we see the other example of an empty eval() function in the below example.

Example

const a = eval();
console.log(a);

Output

undefined

You can see that if we don’t pass anything in the eval function,  it returns undefined.

JavaScript eval() inside a function

If you use the eval() function indirectly by invoking it via a reference other than eval, as of ECMAScript 5, it works in the global scope rather than the local scope. This means, for instance, that function declarations create global functions and that the code being evaluated doesn’t have access to local variables within the scope where it’s being called.

function demo() {
 const x = 4;
 const y = 5;
 const result = eval('x + y');
 return result;
}

const result = demo();
console.log(result);

Output

9

In the above example, we define one demo function. In the demo function, we declare two variables, x, and y. Then we pass them into the eval function, evaluate the expression, store them in the result variable, and return the result.

Store expression in a string

Any unit of code that can be evaluated to a value is an expression in JavaScript. For example, string expressions are expressions that evaluate a string. JavaScript expects a value such as the arguments of a function invocation.

const name = 'paresh';
const str = ` if(name === "paresh"){
 console.log('you are right the name is paresh');
 }else{
 console.log('sorry ! name is wrong');
 }
 `
console.log(eval(str)); 

Output

you are right the name is paresh
undefined

In the above example, we store a whole expression in one string. And the expression contains conditions. And then we passed them into the javascript eval function. So it will print the console of the if block. An eval function does not return anything, so that it will print undefined.

When don’t use eval() function

Usually, we don’t need to use the eval() function in Javascript because in the eval() function, we execute from a string which is a security risk. In modern Javascript, the engine converts the code into machine code and then runs, so we don’t need to use the eval() function.

That’s it for the eval() function in JavaScript.

See also

How to Check undefined in JavaScript

How to Check Null Value in Javascript

Leave a Comment