Comparison with NaN¶
ID: js/comparison-with-nan
Kind: problem
Security severity:
Severity: error
Precision: very-high
Tags:
- reliability
- correctness
- external/cwe/cwe-570
- external/cwe/cwe-571
Query suites:
- javascript-security-and-quality.qls
Click to see the query in the CodeQL repository
Arithmetic comparisons with the special not-a-number value NaN
are useless: nothing is considered to be equal to NaN
, not even NaN
itself, and similarly nothing is considered greater or less than NaN
.
Recommendation¶
Use the isNaN
function from the standard library to determine whether a given value is NaN
.
Example¶
Instead of x === NaN
, use isNaN(x)
.
References¶
Arvind Kumar: Javascript common mistake of comparing with NaN and not with isNaN.
Mozilla Developer Network: NaN.
Common Weakness Enumeration: CWE-570.
Common Weakness Enumeration: CWE-571.