# const

* [JS](https://lochiwei.gitbook.io/web/js) ⟩ [variable](https://lochiwei.gitbook.io/web/js/variable) ⟩ const
* [JS](https://lochiwei.gitbook.io/web/js) ⟩ [declaration](https://lochiwei.gitbook.io/web/js/grammar/declare) ⟩ [variable](https://lochiwei.gitbook.io/web/js/variable/declare) ⟩ const

{% hint style="success" %}
[es6](https://lochiwei.gitbook.io/web/js/feature/es6 "mention")

([declare](https://lochiwei.gitbook.io/web/js/grammar/declare "mention")) declares a <mark style="color:yellow;">**local**</mark>**&#x20;**<mark style="color:orange;">**constant**</mark> in [block](https://lochiwei.gitbook.io/web/js/grammar/statement/other/block/block "mention").
{% endhint %}

{% tabs %}
{% tab title="🧨 雷區" %}
{% hint style="danger" %} <mark style="color:purple;">**const**</mark>&#x20;

* <mark style="color:red;">**must**</mark>**&#x20;**<mark style="color:yellow;">**have**</mark> an [initial-value](https://lochiwei.gitbook.io/web/js/variable/declare/initial-value "mention"):exclamation: (:point\_right: [more](https://lochiwei.gitbook.io/web/js/variable/declare/const/require-initial))
* <mark style="color:red;">**can't**</mark>**&#x20;**<mark style="color:yellow;">**be**</mark> <mark style="color:yellow;">**reassigned**</mark>:exclamation:  (:point\_right: [more](https://lochiwei.gitbook.io/web/js/variable/declare/const/cant-reassign))
* <mark style="color:red;">**can't**</mark>**&#x20;**<mark style="color:yellow;">**be**</mark> [<mark style="color:yellow;">**redeclared**</mark>](https://lochiwei.gitbook.io/web/js/grammar/declare):exclamation:
  {% endhint %}

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

* caused by referencing a [let](https://lochiwei.gitbook.io/web/js/variable/declare/let "mention")/ [const](https://lochiwei.gitbook.io/web/js/variable/declare/const "mention")/ [class](https://lochiwei.gitbook.io/web/js/val/class "mention") in its [tdz](https://lochiwei.gitbook.io/web/js/variable/access/tdz "mention"):exclamation:
  {% endhint %}

{% hint style="info" %}
:white\_check\_mark: it's <mark style="color:yellow;">**common**</mark> to declare <mark style="color:purple;">**const**</mark> using <mark style="color:yellow;">**capital letters**</mark>.
{% endhint %}

{% hint style="danger" %} <mark style="color:yellow;">**using**</mark> [typeof](https://lochiwei.gitbook.io/web/js/val/type/name/typeof "mention") on [lexical-declaration](https://lochiwei.gitbook.io/web/js/grammar/declare/lexical-declaration "mention") ([let](https://lochiwei.gitbook.io/web/js/variable/declare/let "mention")/ [const](https://lochiwei.gitbook.io/web/js/variable/declare/const "mention")/ [class](https://lochiwei.gitbook.io/web/js/val/class "mention")) in its [tdz](https://lochiwei.gitbook.io/web/js/variable/access/tdz "mention") will throw a [<mark style="color:red;">**ReferenceError**</mark>](https://lochiwei.gitbook.io/web/js/err/ref/cannot-access-before-init).&#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="⭐️ 重點" %}
{% hint style="warning" %} <mark style="color:purple;">**const**</mark>&#x20;

* is a [reserved](https://lochiwei.gitbook.io/web/js/grammar/token/keyword/reserved "mention"):exclamation:
* <mark style="color:red;">**must**</mark> be <mark style="color:yellow;">**initialized**</mark> at <mark style="color:yellow;">**declaration**</mark>. 👉 [require-initial](https://lochiwei.gitbook.io/web/js/variable/declare/const/require-initial "mention")
* <mark style="color:red;">**can't**</mark>**&#x20;**<mark style="color:yellow;">**be**</mark> <mark style="color:yellow;">**reassigned**</mark>.  👉 [cant-reassign](https://lochiwei.gitbook.io/web/js/variable/declare/const/cant-reassign "mention")
* <mark style="color:red;">**can't**</mark>**&#x20;**<mark style="color:yellow;">**be**</mark> [<mark style="color:yellow;">**redeclared**</mark>](https://lochiwei.gitbook.io/web/js/grammar/declare).
  {% endhint %}

{% hint style="warning" %} <mark style="color:purple;">**const**</mark>&#x20;

* <mark style="color:green;">**can**</mark>**&#x20;**<mark style="color:yellow;">**be used**</mark> with [in](https://lochiwei.gitbook.io/web/js/grammar/statement/loop/for/in "mention") / [of](https://lochiwei.gitbook.io/web/js/grammar/statement/loop/for/of "mention") loop.
* <mark style="color:red;">**can't**</mark> <mark style="color:yellow;">**be used**</mark> with a <mark style="color:yellow;">**classic for-loop**</mark>.&#x20;

👉 [no-for-loop](https://lochiwei.gitbook.io/web/js/variable/declare/const/no-for-loop "mention"):exclamation:
{% endhint %}
{% endtab %}

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

* [require-initial](https://lochiwei.gitbook.io/web/js/variable/declare/const/require-initial "mention"):exclamation:
* [cant-reassign](https://lochiwei.gitbook.io/web/js/variable/declare/const/cant-reassign "mention"):exclamation:
* [no-for-loop](https://lochiwei.gitbook.io/web/js/variable/declare/const/no-for-loop "mention"):exclamation:
* [tdz](https://lochiwei.gitbook.io/web/js/variable/access/tdz "mention"):exclamation:
  {% endtab %}

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

* compare： [let](https://lochiwei.gitbook.io/web/js/variable/declare/let "mention"), [var](https://lochiwei.gitbook.io/web/js/variable/declare/var "mention")
  {% endtab %}

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

* [ ] [Statements and declarations](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements) ⟩ [declaring variables](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements#declaring_variables) ⟩ [const](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/const)
  {% endtab %}
  {% endtabs %}
