MDN documentation says:
Never use eval()!
Using direct eval()
suffers from multiple problems:
-
eval()
executes the code it's passed with the privileges of the caller. If you runeval()
with a string that could be affected by a malicious party, you may end up running malicious code on the user's machine with the permissions of your webpage / extension. More importantly, allowing third-party code to access the scope in whicheval()
was invoked (if it's a direct eval) can lead to possible attacks that reads or changes local variables. -
eval()
is slower than the alternatives, since it has to invoke the JavaScript interpreter, while many other constructs are optimized by modern JS engines. - Modern JavaScript interpreters convert JavaScript to machine code. This means that any concept of variable naming gets obliterated. Thus, any use of
eval()
will force the browser to do long expensive variable name lookups to figure out where the variable exists in the machine code and set its value. Additionally, new things can be introduced to that variable througheval()
, such as changing the type of that variable, forcing the browser to re-evaluate all of the generated machine code to compensate. - Minifiers give up on any minification if the scope is transitively depended on by
eval()
, because otherwiseeval()
cannot read the correct variable at runtime.