Fork me on GitHub

JSLint Error Explanations

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

Implied eval is evil. Pass a function instead of a string

When do I get this error?

JSLint and JSHint (before version 1.0.0) will throw the "Implied eval is evil. Pass a function instead of a string" error when they encounter a call to the setTimeout or setInterval functions in which the first argument is a string. Here's an example that should pop up a browser alert after one second:

Why do I get this error?

This error is raised to highlight the use of a bad practice and a possible misunderstanding of the language. By passing a string to setTimeout or setInterval you are adding significant extra work for the engine. It has to parse that string as a complete program, akin to the eval function. For full details of why this is a problem, see the article on the related message, "eval is evil".

You can fix this issue by simply passing a function to setTimeout or setInterval. In a situation like that of the example above, you can achieve this by passing an anonymous function:

In JSHint 1.0.0 and above this warning has changed to "Implied eval. Consider passing a function instead of a string". 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