isNaNComparisons
Reports comparisons with NaN, which should use Number.isNaN() instead.
✅ This rule is included in the ts logical presets.
In JavaScript, NaN (Not-a-Number) has a unique property: it is not equal to anything, including itself.
This means that comparisons like x === NaN or x !== NaN always produce unexpected results - the equality check always returns false, and the inequality check always returns true.
To reliably check if a value is NaN, use Number.isNaN() instead.
Examples
Section titled “Examples”if (value === NaN) { console.log("value is NaN");}const isNotANumber = result !== NaN;if (value > NaN) { console.log("comparison result");}if (Number.isNaN(value)) { console.log("value is NaN");}const isNotANumber = !Number.isNaN(result);Options
Section titled “Options”This rule is not configurable.
When Not To Use It
Section titled “When Not To Use It”There is no legitimate reason to compare directly with NaN.
This rule should always be enabled to prevent subtle bugs.
Further Reading
Section titled “Further Reading”Equivalents in Other Linters
Section titled “Equivalents in Other Linters”- Biome:
useIsNan - Deno:
use-isnan - ESLint:
use-isnan - Oxlint:
eslint/use-isnan
Made with ❤️🔥 in Boston by
Josh Goldberg and contributors.