> 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/grammar/statement/loop/for/for/for-init-scope.md).

# "for-init" scope

[JS](/web/js.md)⟩ [syntax](broken://pages/-MkkESuTtsNsTLnJJJUC) ⟩ [for loops](/web/js/grammar/statement/loop/for.md) ⟩ [for](/web/js/grammar/statement/loop/for/for.md) ⟩ "for-init" scope

{% hint style="warning" %}
a <mark style="color:red;">**special**</mark> [scope](/web/js/scope.md) (in a [for](/web/js/grammar/statement/loop/for/for.md) loop) that <mark style="color:yellow;">**lies between**</mark> the <mark style="color:yellow;">**outer scope**</mark> and <mark style="color:yellow;">**inner**</mark> [block scope](/web/js/grammar/statement/other/block/block.md). ( 👉 similar to [function name scope](/web/js/scope/implied.md) )
{% endhint %}

{% tabs %}
{% tab title="⭐️ 重點" %}
{% hint style="warning" %}
雖說似乎有這樣一個奇怪的 <mark style="color:purple;">**for-init scope**</mark>，但裡面 [let](/web/js/variable/declare/let.md) 變數的表現其實更像是 [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="💈範例" %}

* replit：[for-init scope](https://replit.com/@pegasusroe/classic-for-scope#index.js)

```javascript
const { log } = console;

// ----------------------------------------------
//     ⭐️ scopes around a classic for-loop❗
// ----------------------------------------------

// 🔸 (1) outer scope

// 🔸 (2) "init-block" scope
//
//  ┌                                             ┐
//  │  a special "init-block" scope lies between  │
//  │  "outer scope" and "inner block scope"      │
//  └           │                                 ┘
//              │
//  ╭─ (2) init-blk scope ──╮
//  │                       │
for ( let i = 0; i < 3; i++ ) {    // ⭐️ `i` is invisible outside `for`
    
    // 🔸 (3) inner "block scope"
    
    // --------------------------------------------------------
    let i = 100;    // ⭐️ index `i` can be declared again❗
                    // ----------------------------------------
                    // Should (2) and (3) be the same scope, 
                    // this would have been a SyntaxError.
                    // ( `let` doesn't allow redeclarations❗)
    // --------------------------------------------------------
    
    log(i);         // 100, 100, 100
}

// ❗ "var" has no "init-block" scope
//
//  ╭── init-block scope ───╮
for (var j = 10; j < 13; j++) {    // ⭐️ `j` is visible outside `for`❗
    log(j);         // 10, 11, 12
}

// log
[
    // ⭐️ "let" is invisible outside for loop
    // i,           // ⛔ ReferenceError: i is not defined

    // ⭐️ "var" is visible❗
    j,              // 13
    
].forEach(x => console.log(x));
```

{% endtab %}
{% endtabs %}
