Why is using `eval` considered bad in JavaScript?

View other answers to this thread
Balaji Venkiteswaran's photo

Here are some problems

A)Any variables declared in the eval code can pollute the lexical scope . It might shadow the variables that are declared outside the eval as well . It creates global variables and pollutes the scoping model

B)the code is hard to debug

C) Your code becomes slow . Javascript compiler initially does a single pass compilation before running the code . However in case of eval it is compiled and run only in the run time

D)Any malicious code can end up being "eval" ed

Alternatives to eval :

https://www.npmjs.com/package/vm