---
description: declares a block-scoped local variable.
---

# let

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

{% hint style="success" %}
[es6](../../feature/es6 "mention")

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

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

* [typeof-let-const-class-in-tdz-gets-an-error](../access/tdz/typeof-let-const-class-in-tdz-gets-an-error "mention")
{% endhint %}
{% endtab %}

{% tab title="🔴 主題" %}
* :exclamation:[shadows-global-object-property](../../scope/global/variable/let/shadows-global-object-property "mention"):exclamation:
* :white\_check\_mark:[no-redeclare](let/no-redeclare "mention")
* :white\_check\_mark:[let-cant-shadow-param](let/let-cant-shadow-param "mention")
{% endtab %}

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

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

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

* [redeclare](var/redeclare "mention"), even in [strict-mode](../../concept/env/js-engine/mode/strict-mode "mention"):exclamation:
* [no-redeclare](let/no-redeclare "mention"), even in [sloppy-mode](../../concept/env/js-engine/mode/sloppy-mode "mention"):exclamation:
{% endhint %}

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

{% tab title="👥 相關" %}
* :scales:[const](const "mention") / [var](var "mention")
* :no\_entry:[tdz](../access/tdz "mention"):exclamation:
* :exclamation:[param-by-var](var/param-by-var "mention")
* :exclamation:[redeclare](var/redeclare "mention")
{% endtab %}

{% tab title="📗 參考" %}
* [ ] [ydkjs-scope-and-closures-v.2](../../../master/ref/book/you-dont-know-js-series-v.2/ydkjs-scope-and-closures-v.2 "mention") ⟩ 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 %}
