# typeof let/const/class in TDZ gets an error❗️

* [JS](/web/js.md) ⟩ [value](/web/js/val.md) ⟩ [type](/web/js/val/type.md) ⟩ [name](/web/js/val/type/name.md) ⟩ [typeof](/web/js/val/type/name/typeof.md) ⟩ no typeof let/const/class in TDZ
* [JS](/web/js.md) ⟩ [operator](/web/js/grammar/op.md) ⟩ [unary](/web/js/grammar/op/unary.md) ⟩ [typeof](/web/js/val/type/name/typeof.md) ⟩ no typeof let/const/class in TDZ

{% hint style="danger" %} <mark style="color:yellow;">**using**</mark> [typeof](/web/js/val/type/name/typeof.md) <mark style="color:yellow;">**on**</mark> [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 <mark style="color:yellow;">**throw**</mark> an [<mark style="color:red;">**error**</mark>](/web/js/err/ref/cannot-access-before-init.md).
{% endhint %}

{% hint style="danger" %}
:no\_entry: [<mark style="color:red;">**ReferenceError**</mark>](/web/js/err/ref.md)：[<mark style="color:yellow;">**cannot access '...' before initialization**</mark>](/web/js/err/ref/cannot-access-before-init.md):exclamation:
{% endhint %}

{% tabs %}
{% tab title="🧨 雷區" %}
replit：[typeof in TDZ](https://replit.com/@pegasusroe/typeof-in-TDZ#index.js)

```javascript
'use strict';    // ⭐ toggle sloppy/strict mode

const { log } = console;

// ┌───────────────────────┐
// │  temporal dead zone   │
// └───────────────────────┘

// ❗ lexical declarations (let/const/class) are still "uninitialized"
typeof aLet;       // ⛔ ReferenceError
typeof aConst;     // ⛔ ReferenceError
typeof aClass;     // ⛔ ReferenceError
// ⛔ ReferenceError: Cannot access '...' before initialization

// ┌───────────────────────┐
// │ undeclared identifier │
// └───────────────────────┘

// ⭐ can't reference an "undeclared identifier" direcctly.
undeclared;        // ⛔ ReferenceError
// ⛔ ReferenceError: 'undeclared' is not defined

// ⭐ but `typeof` can be used with "undeclared identifier"
typeof undeclared;                        // ❗ 'undefined'

// ❗ "var" has no "temporal dead zone", but it's `undefined` initially.
log(aVar === undefined, typeof aVar);     // ❗ true, 'undefined'

// ┌────────────────────────────────────────┐
// │ lexical declarations (let/const/class) │
// └────────────────────────────────────────┘

let aLet;                                 // ⭐ initialized to `undefined`
const aConst = "hello";
class aClass {}

// ┌─────┐
// │ var │
// └─────┘

// ❗ "var" is a "statement", not "declaration".
var aVar;
```

{% endtab %}

{% tab title="⭐️ 重點" %}
{% hint style="danger" %}

```javascript
typeof null === "object"    // true
```

{% endhint %}
{% endtab %}

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

* :floppy\_disk:[typeName()](/web/js/val/type/name/typename.md) - <mark style="color:yellow;">**refined**</mark> version of <mark style="color:purple;">**typeof**</mark>.
* :u6307:[lexical declaration](/web/js/grammar/declare/lexical-declaration.md) - let / const / class.
* :no\_entry:[cannot access 'xxx' before initialization❗️](/web/js/err/ref/cannot-access-before-init.md)
  {% endtab %}

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

* [typeof](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/typeof)
  {% endtab %}
  {% endtabs %}


---

# Agent Instructions: 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:

```
GET https://lochiwei.gitbook.io/web/js/variable/access/tdz/typeof-let-const-class-in-tdz-gets-an-error.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
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.
