# use-isnan

> Require `Number.isNaN()` instead of a comparison with `NaN`

`NaN` isn't equal to anything, not even itself. `value === NaN` is always `false`, `value !== NaN` is always `true`, and `<`, `>`, `<=` and `>=` with `NaN` are always `false`.

The rule reports comparisons with `NaN` and `Number.NaN`. It also reports `switch (NaN)` and `case NaN:`, which can never match.

For `==`, `===`, `!=` and `!==`, the report comes with a suggestion that rewrites the comparison to `Number.isNaN()`.

This rule is on by default.

{/* Generated by src/codegen/lint-rule-docs.ts. Do not edit this section. */}

## Configure

On by default as an error. To turn it off:

```toml bunfig.toml
[lint.rules]
"use-isnan" = "off" # or "warn"
```

Every option, set to its default:

```toml bunfig.toml
[lint.rules]
"use-isnan" = ["error", {
  enforceForSwitchCase = true,
  enforceForIndexOf = false,
}]
```

{/* End of the generated section. */}

## Examples

Reported:

{/* bad */}
```js
const price = Number(input);

if (price === NaN) {
  showError();
}
```

{/* bad */}
```js
switch (price) {
  case NaN:
    showError();
    break;
  case 0:
    showFree();
    break;
}
```

Not reported:

{/* good */}
```js
const price = Number(input);

if (Number.isNaN(price)) {
  showError();
} else if (price === 0) {
  showFree();
}
```

## Options

| Option | Default | What it does |
| --- | --- | --- |
| `enforceForSwitchCase` | `true` | Report `switch (NaN)` and `case NaN:`. |
| `enforceForIndexOf` | `false` | Also report `indexOf(NaN)` and `lastIndexOf(NaN)` called as methods. They compare with `===`, so they always return `-1`. |

Reported with `enforceForIndexOf`:

{/* bad {"options": [{"enforceForIndexOf": true}]} */}
```js
const hasBadPrice = prices.indexOf(NaN) !== -1;
```

`includes` and `findIndex` can find `NaN`:

{/* good {"options": [{"enforceForIndexOf": true}]} */}
```js
const hasBadPrice = prices.includes(NaN);
const firstBadPrice = prices.findIndex(Number.isNaN);
```
