All Downloads are FREE. Search and download functionalities are using the official Maven repository.

rules.jshint.W019.html Maven / Gradle / Ivy

Go to download

Consume reports generated by jshint for code quality. Also consume reports for code duplication (either simian or cpd). Consumes the unit/integration tests reports (generated by Jasmin) coverage report (lcov generated by Istanbul). The information generated by reports are added in Sonar

There is a newer version: 2.1.0
Show newest version

    

When do I get this error?

The "Use the isNaN function to compare with NaN" error is thrown when JSLint, JSHint and ESLint 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:

x
 
1
var x = parseInt("myString", 10);
2
if (x === NaN) {
3
    x = 10;
4
}
5
JSLint found 1 errorVersion 2015-09-23
Line 1:Use the isNaN function to compare with NaN.

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.
        ...

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:

5
 
1
var x = parseInt("myString", 10);
2
if (isNaN(x)) {
3
    x = 10;
4
}
5
JSLint found no errorsVersion 2015-09-23

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.

In ESLint the rule that generates this warning is named use-isnan. You can disable it by setting it to 0, or enable it by setting it to 1.





© 2015 - 2025 Weber Informatics LLC | Privacy Policy