Fork me on GitHub

JSLint Error Explanations

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

Weird relation

When do I get this error?

JSLint will throw the "Weird relation" error when it encounters a comparison in which the left and right hand sides are "similar", or a comparison in which either the left or right hand side is a string literal and the other is a numeric literal. In the following example we attempt to compare x with itself:

Why do I get this error?

This error is raised to highlight a potentially confusing and potentially pointless piece of code. There are almost no situations in which you would need to compare something to itself.

There is, however, one use case for this kind of comparison. Since the special Number value NaN is never equal to itself, you can use a self-comparison to check whether some value is NaN. The following example will return true only if the value is NaN.

If you are receiving this error for this specific use case, you can either ignore the error and let your script fail the JSLint test, or you can use the built-in isNaN function instead. Here's the above snippet rewritten:

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