Skip to content

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.

if (value === NaN) {
console.log("value is NaN");
}
const isNotANumber = result !== NaN;
if (value > NaN) {
console.log("comparison result");
}

This rule is not configurable.

There is no legitimate reason to compare directly with NaN. This rule should always be enabled to prevent subtle bugs.

Made with ❤️‍🔥 in Boston by Josh Goldberg and contributors.