src/content/docs/linter/rules/use-is-nan.mdx
import { Tabs, TabItem } from '@astrojs/starlight/components';
<Tabs> <TabItem label="JavaScript (and super languages)" icon="seti:javascript"> ## Summary - Rule available since: `v1.0.0` - Diagnostic Category: [`lint/correctness/useIsNan`](/reference/diagnostics#diagnostic-category) - This rule is **recommended**, meaning it is enabled by default. - This rule has an [**unsafe**](/linter/#unsafe-fixes) fix. - The default severity of this rule is [**error**](/reference/diagnostics#error). - Sources: - Same as [`use-isnan`](https://eslint.org/docs/latest/rules/use-isnan){
"linter": {
"rules": {
"correctness": {
"useIsNan": "error"
}
}
}
}
Require calls to isNaN() when checking for NaN.
In JavaScript, 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.
Because NaN is unique in JavaScript by not being equal to anything, including itself, the results of comparisons to NaN are confusing:
NaN === NaN or NaN == NaN evaluate to falseNaN !== NaN or NaN != NaN evaluate to trueTherefore, use Number.isNaN() or global isNaN() functions to test whether a value is NaN.
Note that Number.isNaN() and isNaN() do not have the same behavior.
When the argument to isNaN() is not a number, the value is first coerced to a number.
Number.isNaN() does not perform this coercion.
Therefore, it is a more reliable way to test whether a value is NaN.
123 == NaN
123 != NaN
switch(foo) { case (NaN): break; }
Number.NaN == "abc"
if (Number.isNaN(123) !== true) {}
foo(Number.NaN / 2)
switch(foo) {}