Fork me on GitHub

JSLint Error Explanations

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

Move the invocation into the parens that contain the function

When do I get this error?

JSLint will throw the "Move the invocation into the parens that contain the function" error when it encounters an immediately invoked function expression in which the invoking parentheses appear outside the wrapping parentheses. In the following example we assign the return value of the anonymous function to the variable x:

Why do I get this error?

This error is raised to highlight a lack of convention. Your code will not run without error if you do not change it, but your code may be confusing to others. However, this particular error gives rise to debate over which position for the invoking parentheses should actually be the convention. If you are using JSLint, then your convention should be the one suggested.

The argument for moving the parentheses is that it makes the code easier to understand. It may not be immediately obvious that the parentheses are intended to invoke the function if they are outside the wrapping pair. By moving them inside, they immediately follow the function statement and may therefore be more easily associated with it.

To fix this error, simply move the pair of invoking parentheses inside the pair that wrap the function itself:

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