console.log(eval("2 + 2")); // 4
When you pass either an expression as in the example above, or a statement as seen below:
// undefined because statement doesn't produce value console.log(eval('let declaredVariable = 7')); // undefined
Should you use eval() function?
eval() function is created so that you can turn a
eval() operation, meaning if the
string contains code that manipulates your database, then there’s no way to stop that code.
This is why the
eval() function is considered evil and should be avoided.