> 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/var/var-in-block-cant-shadow-outer-let.md).

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

🚧 under construction

[JS](/web/js.md) ⟩ [variable](/web/js/variable.md) ⟩ [shadowing](/web/js/variable/shadow.md) ⟩ 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)
* 👉 [⛔️ SyntaxError](/web/js/err/syntax.md) ⟩ [identifier 'xxx' has already been declared❗️](/web/js/err/syntax/redeclare.md)

```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="👥 相關" %}

* [var has no block scope❗️](/web/js/variable/declare/var/in-block-cant-shadow.md):exclamation:
* one of the reasons of [stop using var❗️](/web/js/variable/declare/var/stop-using-var.md)
  {% 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 & Closures (v.2)](/web/master/ref/book/you-dont-know-js-series-v.2/ydkjs-scope-and-closures-v.2.md) ⟩ Ch. 8 ⟩ [Parameter Scope](https://github.com/getify/You-Dont-Know-JS/blob/2nd-ed/scope-closures/apA.md#parameter-scope)
  {% endtab %}
  {% endtabs %}
