> 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/variable/declare/let.md).

# let

declares a block-scoped local variable.

* [JS](/web/js.md) ⟩ [variable](/web/js/variable.md) ⟩ let
* [JS](/web/js.md) ⟩ [declaration](/web/js/grammar/declare.md) ⟩ [variable](/web/js/variable/declare.md) ⟩ let

{% hint style="success" %}
[⭐️ ES6 (2015)](/web/js/feature/es6.md)

([declaration](/web/js/grammar/declare.md)) <mark style="color:yellow;">**declares**</mark> a <mark style="color:yellow;">**local**</mark> [variable](/web/js/variable.md) in [block scope](/web/js/grammar/statement/other/block/block.md).
{% endhint %}

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

* [typeof let/const/class in TDZ gets an error❗️](/web/js/variable/access/tdz/typeof-let-const-class-in-tdz-gets-an-error.md)
  {% endhint %}
  {% endtab %}

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

* :exclamation:[global let shadows global object property](/web/js/scope/global/variable/let/shadows-global-object-property.md):exclamation:
* :white\_check\_mark:[let redeclaration not allowed even in sloppy mode❗️](/web/js/variable/declare/let/no-redeclare.md)
* :white\_check\_mark:[let can't shadow parameter even in sloppy mode❗️](/web/js/variable/declare/let/let-cant-shadow-param.md)
  {% endtab %}

{% tab title="⭐️ 重點" %}
{% hint style="danger" %} <mark style="color:purple;">**let**</mark> is&#x20;

* a [contextual keyword](/web/js/grammar/token/keyword/contextual.md), &#x20;
* <mark style="color:red;">**not**</mark> [reserved word](/web/js/grammar/token/keyword/reserved.md):exclamation:
  {% endhint %}

{% hint style="warning" %}
JS is <mark style="color:green;">**lenient**</mark> with [var](/web/js/variable/declare/var.md), <mark style="color:red;">**strict**</mark> with [let](/web/js/variable/declare/let.md).

* [var redeclaration applied even in strict mode❗️](/web/js/variable/declare/var/redeclare.md), even in [strict mode](/web/js/concept/env/js-engine/mode/strict-mode.md):exclamation:
* [let redeclaration not allowed even in sloppy mode❗️](/web/js/variable/declare/let/no-redeclare.md), even in [sloppy mode](/web/js/concept/env/js-engine/mode/sloppy-mode.md):exclamation:
  {% endhint %}

{% hint style="warning" %}
雖說似乎有一個奇怪的 ["for-init" scope](/web/js/grammar/statement/loop/for/for/for-init-scope.md)，但裡面 <mark style="color:purple;">**let**</mark> 變數的表現其實更像是 [block-scoped](/web/js/grammar/statement/other/block/block.md)。:point\_right: [scopes matter with closures](/web/js/val/func/closure/scopes-matter-with-closures.md) ⟩ [scopes matter with closures](/web/js/val/func/closure/scopes-matter-with-closures.md#undefined-1)
{% endhint %}
{% endtab %}

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

* :scales:[const](/web/js/variable/declare/const.md) / [var](/web/js/variable/declare/var.md)
* :no\_entry:[temporal dead zone](/web/js/variable/access/tdz.md):exclamation:
* :exclamation:[var can shadow parameter even in strict mode❗️](/web/js/variable/declare/var/param-by-var.md)
* :exclamation:[var redeclaration applied even in strict mode❗️](/web/js/variable/declare/var/redeclare.md)
  {% endtab %}

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

* [ ] [YDKJS: Scope & Closures (v.2)](/web/master/ref/book/you-dont-know-js-series-v.2/ydkjs-scope-and-closures-v.2.md) ⟩ Ch. 3: [The Scope Chain](https://github.com/getify/You-Dont-Know-JS/blob/2nd-ed/scope-closures/ch3.md#chapter-3-the-scope-chain)
  {% 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) ⟩ [let](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/let)
  {% endtab %}
  {% endtabs %}
