Fork me on GitHub

JSLint Error Explanations

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


Unexpected '++'

When do I get this error?

JSLint will throw the "Unexpected '++'" error when it encounters the use of the increment operator ++. It will throw the related "Unexpected '--'" error when it encounters the use of the decrement operator -- Here's an example:

Why do I get this error?

This error message is perhaps the most debated of all JSLint error messages. It exists solely to warn you that JSLint has encountered a violation of a specific coding style, in particular, the style of the author of JSLint, Douglas Crockford. For his reasoning, you can read the JSLint documentation:

The ++ (increment) and -- (decrement) operators have been known to contribute to bad code by encouraging excessive trickiness. They are second only to faulty architecture in enabling to viruses and other security menaces. Also, preincrement/postincrement confusion can produce off-by-one errors that are extremely difficult to diagnose.

There are many JavaScript developers who disagree with that, but the fact remains, it's a rule in JSLint so you need a way to work around it. What JSLint would prefer you to do is use the normal addition and subtraction operators, which can be combined with the assignment operator for a slight decrease in length:

If you would prefer not to do that, and would rather stick with the normal increment and decrement operators, you can set the plusplus option to true to tell JSLint to allow them:


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