Fork me on GitHub

JSLint Error Explanations

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


The Function constructor is eval

When do I get this error?

JSLint and JSHint (before version 1.0.0) will throw the "The Function constructor is eval" error when they encounter a call to the Function constructor function preceded by the new operator. Here's a simple example which defines a function to add two numbers:

Why do I get this error?

This error is raised to highlight the use of a bad practice. By passing a string to the Function constructor, you are requiring the engine to parse that string much in the way it has to when you call the eval function. For full details of why this is a problem, see the article on the related message, "eval is evil".

In simple cases like that of our example above, you can fix the issue by using a function declaration or function expression:

In more advanced cases where you really need to use the Function constructor, you can set the evil option to true to prevent both JSLint and JSHint from complaining about it:

In JSHint 1.0.0 and above this warning has changed to "The Function constructor is a form of eval". More detail can be found the page dedicated to that message.


comments powered by Disqus