Fork me on GitHub

JSLint Error Explanations

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

Do not wrap function literals in parens unless they are to be immediately invoked

When do I get this error?

JSLint will throw the "Do not wrap function literals in parens unless they are to be immediately invoked" error when it encounters a function expression wrapped in parentheses with no following invoking parentheses. JSHint (before version 1.0.0) will throw this error in the same situation, but only if the immed option is set to true. In the following example we assign a function expression to a variable x:

Why do I get this error?

This error is raised to highlight a potentially confusing piece of code. It is common in JavaScript to see immediately invoked function expressions. Here's the above snippet again, this time with the invoking parentheses. Notice how subtle the difference is:

While the difference may not look like much, the two snippets are completely different in their behaviour. The first example (which does not pass JSLint) will result in a function expression assigned to x. The second snippet will result in the return value of that function assigned to x.

When you see a function expression wrapped in parentheses, you would usually expect it to be an immediately invoked function expression. When it isn't, there is greater potential for confusion and misunderstanding of your code.

In JSHint 1.0.0 and above this warning has changed to "Wrapping non-IIFE function literals in parens is unnecessary". 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