useIsNan
Summary
Section titled “Summary”- Rule available since:
v1.0.0 - Diagnostic Category:
lint/correctness/useIsNan - This rule is recommended, which means is enabled by default.
- This rule has an unsafe fix.
- The default severity of this rule is error.
- Sources:
- Same as
use-isnan
- Same as
How to configure
Section titled “How to configure”{ "linter": { "rules": { "correctness": { "useIsNan": "error" } } }}Description
Section titled “Description”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===NaNorNaN==NaNevaluate to falseNaN!==NaNorNaN!=NaNevaluate to true
Therefore, 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.
Examples
Section titled “Examples”Invalid
Section titled “Invalid”123 == NaNcode-block.js:1:1 lint/correctness/useIsNan FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
✖ Use the Number.isNaN function to compare with NaN.
> 1 │ 123 == NaN
│ ^^^^^^^^^^
2 │
ℹ Unsafe fix: Use Number.isNaN() instead.
1 │ - 123·==·NaN
1 │ + Number.isNaN(123)
2 2 │
123 != NaNcode-block.js:1:1 lint/correctness/useIsNan FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
✖ Use the Number.isNaN function to compare with NaN.
> 1 │ 123 != NaN
│ ^^^^^^^^^^
2 │
ℹ Unsafe fix: Use Number.isNaN() instead.
1 │ - 123·!=·NaN
1 │ + !Number.isNaN(123)
2 2 │
switch(foo) { case (NaN): break; }code-block.js:1:20 lint/correctness/useIsNan ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
✖ ‘case NaN’ can never match. Use Number.isNaN before the switch.
> 1 │ switch(foo) { case (NaN): break; }
│ ^^^^^
2 │
Number.NaN == "abc"code-block.js:1:1 lint/correctness/useIsNan FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
✖ Use the Number.isNaN function to compare with NaN.
> 1 │ Number.NaN == “abc”
│ ^^^^^^^^^^^^^^^^^^^
2 │
ℹ Unsafe fix: Use Number.isNaN() instead.
1 │ - Number.NaN·==·“abc”
1 │ + Number.isNaN(“abc”)
2 2 │
if (Number.isNaN(123) !== true) {}
foo(Number.NaN / 2)
switch(foo) {}Related links
Section titled “Related links”Copyright (c) 2023-present Biome Developers and Contributors.