# var has no block scope❗️

[JS](../../..) ⟩ [variable](../..) ⟩ [var]() ⟩ has no block scope

{% hint style="danger" %}
<mark style="color:purple;">**var in block**</mark> <mark style="color:red;">**doesn't**</mark>**&#x20;**<mark style="color:yellow;">**have**</mark> [block](../../../grammar/statement/other/block/block "mention"), <mark style="color:red;">**cannot**</mark>**&#x20;**<mark style="color:yellow;">**shadow**</mark> [let](../let "mention") variable in the <mark style="color:yellow;">**outer enclosing scope**</mark>.
{% endhint %}

{% tabs %}
{% tab title="👥 相關" %}
* [shadow](../../shadow "mention")
* [var-in-block-cant-shadow-outer-let](var-in-block-cant-shadow-outer-let "mention"):exclamation:
* [var](../../../scope/hoist/variable/var "mention") - [](<> "mention") is hoisted to top of enclosing [function](../../../scope/function "mention").
* [no-redeclare](../let/no-redeclare "mention") - [let](../let "mention") doesn't allow [redeclaration](..).
{% endtab %}

{% 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="📘 手冊" %}
* [ ] [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) ⟩ [var](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/var)
{% endtab %}
{% endtabs %}

