> For the complete documentation index, see [llms.txt](https://lochiwei.gitbook.io/web/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://lochiwei.gitbook.io/web/js/val/prim/num/special/nan.md).

# NaN

* [JS](/web/js.md) ⟩ [value](/web/js/val.md) ⟩ [primitive](/web/js/val/prim.md) ⟩ [number](/web/js/val/prim/num.md) ⟩ [special](/web/js/val/prim/num/special.md) ⟩ NaN
* [JS](/web/js.md) ⟩ [scope](/web/js/scope.md) ⟩ [global](/web/js/scope/global.md) ⟩ [global object](/web/js/scope/global/object.md) ⟩ [property](/web/js/scope/global/object/prop.md) ⟩ 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> [global object property](/web/js/scope/global/object/prop.md) <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>](/web/js/val/prim/num/special/nan.md)❗

```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>](/web/js/val/type/convert.md) <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>](/web/js/val/type/convert.md).
  {% 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 operator](/web/js/grammar/op/arithmetic.md)(s)

* [<mark style="color:red;">**0**</mark><mark style="color:purple;">**/**</mark><mark style="color:red;">**0**</mark>](/web/js/grammar/op/arithmetic/binary/division.md) <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](/web/js/grammar/op/term/non-numeric-operand.md)(s) <mark style="color:yellow;">**in**</mark> [<mark style="color:orange;">**arithmetic**</mark>](/web/js/grammar/op/arithmetic.md) <mark style="color:yellow;">**operations**</mark> that <mark style="color:red;">**cannot**</mark>**&#x20;**<mark style="color:yellow;">**convert to**</mark> [**numbers**](/web/js/val/prim/num.md) <mark style="color:yellow;">**convert**</mark> to <mark style="color:purple;">**NaN**</mark>.&#x20;
  {% endhint %}

{% hint style="success" %}
[comparison operator](/web/js/grammar/op/relational/compare.md)

<mark style="color:red;">**if**</mark> <mark style="color:yellow;">**either operand**</mark> is (or <mark style="color:blue;">**converts**</mark> to) [NaN](/web/js/val/prim/num/special/nan.md), 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>](/web/js/val/prim/num/special/infinity.md) 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 operator](/web/js/grammar/op/arithmetic/bitwise.md).
{% endhint %}
{% endtab %}

{% tab title="👥 相關" %}

* :point\_right: [Infinity](/web/js/val/prim/num/special/infinity.md)
  {% 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](/web/master/ref/javascript-the-definitive-guide.md) ⟩ 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 %}


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://lochiwei.gitbook.io/web/js/val/prim/num/special/nan.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
