> 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/declare/lexical-declaration.md).

# lexical declaration

🚧 施工中

[JS](/web/js.md) ⟩ [grammar](/web/js/grammar.md) ⟩ [declaration](/web/js/grammar/declare.md) ⟩ <mark style="color:purple;">**lexical declaration**</mark>

{% hint style="warning" %} <mark style="color:yellow;">**lexical declarations**</mark> ([let](/web/js/variable/declare/let.md)/ [const](/web/js/variable/declare/const.md)/ [class](/web/js/val/class.md)):question:
{% endhint %}

{% tabs %}
{% tab title="🧨 雷區" %}
{% hint style="danger" %} <mark style="color:yellow;">**using**</mark> [typeof](/web/js/val/type/name/typeof.md) on [lexical declaration](/web/js/grammar/declare/lexical-declaration.md) ([let](/web/js/variable/declare/let.md)/ [const](/web/js/variable/declare/const.md)/ [class](/web/js/val/class.md)) in its [temporal dead zone](/web/js/variable/access/tdz.md) will throw a [<mark style="color:red;">**ReferenceError**</mark>](/web/js/err/ref/cannot-access-before-init.md).&#x20;

```javascript
// ⭐ temporal dead zone (start)
// ------------------------------
// typeof aLet;       // ⛔ ReferenceError: Cannot access 'aLet' before initialization
// typeof aConst;     // ⛔ ReferenceError: Cannot access 'aConst' before initialization
// typeof aClass;     // ⛔ ReferenceError: Cannot access 'aClass' before initialization

typeof undeclared;        // ❗ 'undefined'

// ⭐ lexical declarations (let/const/class)
// --------------------------------------------
let aLet;                 // ⭐ initialized to `undefined`
const aConst = "hello";
class aClass {}
```

* replit：[TDZ: let/const/class](https://replit.com/@pegasusroe/TDZ-letconstclass#index.js)
  {% endhint %}
  {% endtab %}

{% tab title="🔴 主題" %}

* [temporal dead zone](/web/js/variable/access/tdz.md)
* <mark style="color:red;">**SyntaxError**</mark>：[lexical declaration cannot appear in a single-statement context❗️](/web/js/err/syntax/lexical-declaration-cannot-appear-in-a-single-statement-context.md):exclamation:
  {% endtab %}
  {% endtabs %}
