---
description: not a number.
---

# NaN

* [JS](../../../..) ⟩ [value](../../..) ⟩ [primitive](../..) ⟩ [number](..) ⟩ [special]() ⟩ NaN
* [JS](../../../..) ⟩ [scope](../../../../scope) ⟩ [global](../../../../scope/global) ⟩ [global object](../../../../scope/global/object) ⟩ [property](../../../../scope/global/object/prop) ⟩ NaN

{% hint style="success" %}
<mark style="color:yellow;">**(**</mark>**&#x20;**<mark style="color:red;">**non-configurable**</mark><mark style="color:yellow;">**,**</mark>**&#x20;**<mark style="color:red;">**non-writable**</mark> [prop](../../../../scope/global/object/prop "mention") <mark style="color:yellow;">**)**</mark>:exclamation: &#x20;

* a property of the global object. (that is, a variable in global scope).
* <mark style="color:yellow;">**equivalent to**</mark> [Number.NaN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/NaN).
{% endhint %}

{% tabs %}
{% tab title="🧨 雷區" %}
{% hint style="danger" %}
<mark style="color:purple;">**`NaN`**</mark> is the <mark style="color:red;">**only**</mark>**&#x20;**<mark style="color:yellow;">**value**</mark> that <mark style="color:red;">**doesn't**</mark> <mark style="color:red;">**equal**</mark>**&#x20;**<mark style="color:yellow;">**to itself**</mark>:exclamation:

```javascript
// ⭐️ `NaN` is the ONLY ONE that doesn't equal to itself.
NaN === NaN,            // false❗️
NaN !== NaN,            // true❗️

// equivalent forms
Number.isNaN(x) === (x !== x)
```
{% endhint %}

{% hint style="danger" %}
<mark style="color:yellow;">**trichotomy law**</mark>： <mark style="color:blue;">`a < b, a = b, a > b`</mark> <mark style="color:red;">**doesn't**</mark>**&#x20;**<mark style="color:yellow;">**apply**</mark> to [<mark style="color:blue;">**NaN**</mark>](nan)❗

```javascript
3 <  NaN,      // false❗
3 == NaN,      // false❗
3 >  NaN,      // false❗
```
{% endhint %}
{% endtab %}

{% tab title="⭐️ 重點" %}
{% hint style="success" %}
* <mark style="color:blue;">`isNaN(x)`</mark> will try to [<mark style="color:red;">**convert**</mark>](../../../type/convert) <mark style="color:blue;">`x`</mark> to a <mark style="color:yellow;">**number**</mark> first:exclamation:
* <mark style="color:blue;">`Number.isNaN(x)`</mark> <mark style="color:red;">**never**</mark>**&#x20;**<mark style="color:yellow;">**do**</mark> [<mark style="color:yellow;">**conversions**</mark>](../../../type/convert).
{% endhint %}

{% hint style="warning" %}
for <mark style="color:blue;">`Number.isNaN(x)`</mark> to be <mark style="color:green;">**true**</mark>, <mark style="color:blue;">`x`</mark> <mark style="color:red;">**must**</mark>**&#x20;**<mark style="color:yellow;">**be a number**</mark> first❗️
{% endhint %}

{% hint style="info" %}
<mark style="color:blue;">`x !=== x`</mark> is <mark style="color:yellow;">**equivalent**</mark> to <mark style="color:blue;">`Number.isNaN(x)`</mark>
{% endhint %}

{% hint style="warning" %}
[arithmetic](../../../../grammar/op/arithmetic "mention")(s)

* [<mark style="color:red;">**0**</mark><mark style="color:purple;">**/**</mark><mark style="color:red;">**0**</mark>](../../../../grammar/op/arithmetic/binary/division) <mark style="color:yellow;">**evaluates**</mark> to <mark style="color:purple;">**NaN**</mark> (<mark style="color:red;">**no**</mark>**&#x20;**<mark style="color:yellow;">**error**</mark>):exclamation:
* [non-numeric-operand](../../../../grammar/op/term/non-numeric-operand "mention")(s) <mark style="color:yellow;">**in**</mark> [<mark style="color:orange;">**arithmetic**</mark>](../../../../grammar/op/arithmetic) <mark style="color:yellow;">**operations**</mark> that <mark style="color:red;">**cannot**</mark>**&#x20;**<mark style="color:yellow;">**convert to**</mark> [**numbers**](..) <mark style="color:yellow;">**convert**</mark> to <mark style="color:purple;">**NaN**</mark>.&#x20;
{% endhint %}

{% hint style="success" %}
[compare](../../../../grammar/op/relational/compare "mention")

<mark style="color:red;">**if**</mark> <mark style="color:yellow;">**either operand**</mark> is (or <mark style="color:blue;">**converts**</mark> to) [nan](nan "mention"), the **result** <mark style="color:yellow;">**is**</mark> <mark style="color:red;">**false**</mark>.
{% endhint %}

{% hint style="info" %}
<mark style="color:purple;">**NaN**</mark>, <mark style="color:yellow;">**±**</mark>[<mark style="color:blue;">**Infinity**</mark>](infinity) all <mark style="color:yellow;">**converts**</mark> to <mark style="color:red;">**0**</mark> when used <mark style="color:yellow;">**as operands**</mark> of [bitwise](../../../../grammar/op/arithmetic/bitwise "mention").
{% endhint %}
{% endtab %}

{% tab title="👥 相關" %}
* :point\_right: [infinity](infinity "mention")
{% endtab %}

{% tab title="💈範例" %}
* replit： [NaN](https://replit.com/@pegasusroe/NaN-1#index.js)

```javascript
// ⭐️ `NaN` is the ONLY ONE that doesn't equal to itself❗️
// -------------------------------------------------------
// 
//     `x !=== x` is equivalent to `Number.isNaN(x)`
//
NaN === NaN,            // false❗️
NaN !== NaN,            // true❗️
Number.isNaN(NaN),      // true    (⭐️ the ONLY value that makes this true❗️)

// ⭐️ isNaN() tries to convert to a number first❗️
Number('foo'),          // NaN
isNaN('foo'),           // true❗️  ('foo' -> NaN)
Number('123abc'),       // NaN
isNaN('123abc'),        // true❗️  ('123abc' -> NaN)

// ⭐️ Number.isNaN() NEVER converts anything❗️
// --------------------------------------------
// for `Number.isNaN(x)` to be true, `x` must be a number first❗️
Number.isNaN('foo'),    // false❗️ (⭐️ `false` for non-numeric values❗️)
Number.isNaN(2 / 'foo'),   // true
```
{% endtab %}

{% tab title="📗 參考" %}
* [ ] [javascript-the-definitive-guide](../../../../../master/ref/javascript-the-definitive-guide "mention") ⟩ 4.8
{% endtab %}

{% tab title="📘 手冊" %}
* [ ] [NaN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/NaN)
* [ ] [Number.NaN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/NaN) - equivalent to NaN.
* [ ] [isNaN()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/isNaN) - with type conversion. (attempts to convert to a number)
* [ ] [Number.isNaN()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/isNaN) ⟩ - without type conversion.
  * [ ] [Difference between Number.isNaN() and global isNaN()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/isNaN#difference_between_number.isnan_and_global_isnan)
{% endtab %}
{% endtabs %}

