Fork me on GitHub

JSLint Error Explanations

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


Use the isNaN function to compare with NaN

When do I get this error?

JSLint and JSHint will throw the "Use the isNaN function to compare with NaN" error when they encounter a comparison in which one side is NaN. In the following example we attempt to convert a string into a number with the parseInt function, which returns NaN when it can't perform such a conversion:

Why do I get this error?

This error is raised to highlight code that doesn't work as you expect it to. Your code will run without error, but will not behave as you expect.

NaN is a special value of the Number type. It's used to represent any of the "not-a-number" values represented by the double-precision 64-bit format as specified by the IEEE Standard for Binary Floating-Point Arithmetic. NaN has the unique property of not being equal to anything, including itself. That is to say, that the condition NaN !== NaN evaluates to true.

The strict equality comparison algorithm (ES5 §11.9.6) specifically handles the NaN value:

The comparison x === y, where x and y are values, produces true or false. Such a comparison is performed as follows:
...
4. If Type(x) is Number then
    a. If x is NaN, return false.
    b. If y is NaN, return false.
    ...

Note that the abstract equality comparison algorithm (ES5 §11.9.3) behaves in exactly the same way. This means that when you attempt to compare something to NaN, the condition will always evaluate to false.

To fix this error, as the message suggests, you can use the isNaN function, which is a built-in property of the global object. It's defined in ES5 § 15.1.2.4 and simply returns true if its argument coerces to NaN, and false if it does not:

In JSHint 1.0.0 and above you have the ability to ignore any warning with a special option syntax. The identifier of this warning is W019. This means you can tell JSHint to not issue this warning with the /*jshint -W019 */ directive.


comments powered by Disqus