> 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/grammar/token/id/undeclared.md).

# undeclared identifier

[JS](/web/js.md) ⟩ [identifier](/web/js/grammar/token/id.md) ⟩ undeclared

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

* [assignment (=)](/web/js/grammar/op/assign/assignment.md) to an <mark style="color:purple;">**undeclared identifier**</mark> in [sloppy mode](/web/js/concept/env/js-engine/mode/sloppy-mode.md) will result in an [accidental global variable in sloppy mode❗️](/web/js/concept/env/js-engine/mode/sloppy-mode/accidental-global-variable-in-sloppy-mode.md)
* [temporal dead zone](/web/js/variable/access/tdz.md)
  {% endtab %}

{% tab title="💈範例" %}

* 👉 related：[class body has its own "this" context](https://replit.com/@pegasusroe/class-body-has-this-context#index.js)

```javascript
typeof undeclared === "undefined"     // true
```

{% endtab %}

{% tab title="🧨 雷區" %}
{% hint style="danger" %}
[assignment (=)](/web/js/grammar/op/assign/assignment.md) to [undeclared identifier](/web/js/grammar/token/id/undeclared.md) will result in&#x20;

* [accidental global variable in sloppy mode❗️](/web/js/concept/env/js-engine/mode/sloppy-mode/accidental-global-variable-in-sloppy-mode.md) (<mark style="color:yellow;">**in**</mark> [sloppy mode](/web/js/concept/env/js-engine/mode/sloppy-mode.md))
* :no\_entry:️ [<mark style="color:red;">**ReferenceError**</mark>](/web/js/err/ref.md)：[<mark style="color:yellow;">**'...' is not defined**</mark>](/web/js/err/ref/not-defined.md):exclamation: (<mark style="color:yellow;">**in**</mark> [strict mode](/web/js/concept/env/js-engine/mode/strict-mode.md))
  {% endhint %}

{% hint style="warning" %}
there is <mark style="color:red;">**NO way**</mark> to write a <mark style="color:yellow;">**function**</mark> to <mark style="color:orange;">**check if an identifer is declared**</mark>.

```javascript
// assume f is such a function
f(undeclared)          // ⛔ ReferenceError

// JS will throw a ReferenceError on accessing `undeclared`
// BEFORE you even have a chance to call f()❗
```

{% endhint %}
{% endtab %}

{% tab title="📗 參考" %}

* [ ] [3 Ways to Check if a Variable is Defined in JavaScript](https://dmitripavlutin.com/javascript-defined-variable-checking/) ⭐️
  {% endtab %}

{% tab title="📘 手冊" %}

* [ ] [typeof](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/typeof) ⟩ [Interaction with undeclared and uninitialized variables](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/typeof#interaction_with_undeclared_and_uninitialized_variables)
* [ ] [ReferenceError: "x" is not defined](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Errors/Not_defined)
  {% endtab %}

{% tab title="🗣 討論" %}

* [JavaScript check if variable exists (is defined/initialized)](https://stackoverflow.com/a/519157/5409815)
  {% endtab %}
  {% endtabs %}
