Comparison with NaN¶
ID: js/comparison-with-nanKind: problemSecurity severity: Severity: errorPrecision: very-highTags: - quality - reliability - correctness - external/cwe/cwe-570 - external/cwe/cwe-571Query suites: - javascript-code-quality.qls - javascript-security-and-quality.qls
Click to see the query in the CodeQL repository
Arithmetic comparisons with the special not-a-number valueNaN are useless: nothing is considered to be equal toNaN, not evenNaN itself, and similarly nothing is considered greater or less thanNaN.
Recommendation¶
Use theisNaN function from the standard library to determine whether a given value isNaN.
Example¶
Instead ofx===NaN, useisNaN(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.