Fork me on GitHub

JSLint Error Explanations

JSLint will hurt your feelings. It's time to make them better!

eval is evil

When do I get this error?

JSLint and JSHint (prior to version 1.0.0) will throw the "eval is evil" error when they encounter an invocation of the eval function. Here's an example in which we use eval to access an object property:

Why do I get this error?

There are numerous reasons for this error. Some of the major ones include potentially dangerous code and a likelihood of a misunderstanding of the language. It can also indicate slow, inefficient code. For more details, Angus Croll's article How evil is eval? is highly recommended.

It's well-documented that eval is probably the most misused feature of JavaScript. The main reason for this is that there is almost always a better way to achieve the same thing. It's quite likely that if you're reading this, you are trying to do something like our example above - access an object property with a string. There's a much better way to do that, using the square bracket syntax to access properties with a variable.:

The eval function is slow. If you're using it unecessarily, you're slowing down your program for no reason. The cause of this is the fact that the engine has to parse the argument as a complete new program (ES5 §

When the eval function is called with one argument x, the following steps are taken:

    1. If Type(x) is not String, return x.
    2. Let prog be the ECMAScript code that is the result of parsing x as a Program.

Because eval therefore allows for arbitrary execution of a complete JavaScript program, it can also result in difficult debugging. If you are running large amounts of code through eval you will not get useful errors. The line numbers reported in the errors will correspond to the call to eval only, and not the actual code that caused the problem.

However, in the situation where you absolutely have to use eval, you can tell both JSLint and JSHint to allow it. But you should only do this as a last resort. Just set the evil option to true:

In JSHint 1.0.0 and above this warning has changed to "eval can be harmful". More detail can be found the page dedicated to that message.

James Allardice

This article was written by James Allardice, an enthusiastic young JavaScript developer at Global Personals (we're looking for developers so please apply). He is passionate about writing clean, maintainable JavaScript and uses JSHint every day to help achieve this.

You can follow him on Twitter, fork him on GitHub and find him on Google+.

Proceeds generated by this site are donated to help advance other open source projects

comments powered by Disqus