---
description: 🚧 under construction
---

# var in block can't shadow outer let❗️

[JS](../../..) ⟩ [variable](../..) ⟩ [shadowing](../../shadow) ⟩ var in block can't shadow outer let

{% hint style="warning" %}

{% endhint %}

{% tabs %}
{% tab title="💈範例" %}
* replit：[var in block can't shadow outer let](https://replit.com/@pegasusroe/let-doesnt-allow-redeclaration-1#index.js)
* 👉 [syntax](../../../err/syntax "mention") ⟩ [redeclare](../../../err/syntax/redeclare "mention")

```javascript
function f() {
    
    let a = 1;       // let declaration
    
    // var a = 2;    // <----- var declaration is hoisted here❗️
                     //        which raises a redeclaration SyntaxError.
                     // (let doesn't allow redeclaration)
    
    // block scope
    {
        // ⭐️ `var` doesn't have block scope, 
        //     cannot shadow `let` variable in outer enclosing scope.
        var a = 2;
        //  ^
        // ⛔ SyntaxError: Identifier 'a' has already been declared
    }
    
    // another block
    {
        let b = 3;

        // ❗ although `var` doesn't have "block scope", 
        //    we can't put let/var in the same block either.
        var b = 4;
        //  ^
        // ⛔ SyntaxError: Identifier 'b' has already been declared
    }
    
}
```
{% endtab %}

{% tab title="👥 相關" %}
* [in-block-cant-shadow](in-block-cant-shadow "mention"):exclamation:
* one of the reasons of [stop-using-var](stop-using-var "mention")
{% endtab %}

{% tab title="🗣 討論" %}
* [shadowing parameters by local variables?](https://stackoverflow.com/questions/74154964/shadowing-parameters-by-local-variables) (my question)
{% 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. 8 ⟩ [Parameter Scope](https://github.com/getify/You-Dont-Know-JS/blob/2nd-ed/scope-closures/apA.md#parameter-scope)
{% endtab %}
{% endtabs %}
