What is the eval() Function in JavaScript

JavaScript eval() is a built-in function that “evaluates or executes an argument”. The function accepts a string as an argument, evaluates it as an expression, and returns the result.




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.


const a = eval(new String('4 + 6'));
const b = eval('4 + 6');


4 + 6

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.

Remember that using eval() can introduce security vulnerabilities and performance issues in your code. Therefore, be cautious and consider alternative solutions before resorting to eval().

Leave a Comment