> 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/const/no-for-loop.md).

# no const for classic "for"

[JS](/web/js.md) ⟩ [statement](/web/js/grammar/statement.md) ⟩ [declaration](/web/js/grammar/declare.md) ⟩ [const](/web/js/variable/declare/const.md) ⟩ no const for classic for-loop

{% hint style="danger" %}
[const](/web/js/variable/declare/const.md)&#x20;

* <mark style="color:green;">**can**</mark>**&#x20;**<mark style="color:yellow;">**be used**</mark> with [for-in](/web/js/grammar/statement/loop/for/in.md) / [for-of](/web/js/grammar/statement/loop/for/of.md) loop.
* <mark style="color:red;">**can't**</mark> <mark style="color:yellow;">**be used**</mark> with a <mark style="color:yellow;">**classic**</mark> [for](/web/js/grammar/statement/loop/for/for.md) <mark style="color:yellow;">**loop**</mark>.&#x20;

👉 [no const for classic "for"](/web/js/variable/declare/const/no-for-loop.md):exclamation:
{% endhint %}

{% tabs %}
{% tab title="💈範例" %}

* replit：[classic for-loop can't use const](https://replit.com/@pegasusroe/classic-for-loop-cant-use-const#index.js)
* 👉 [⛔️ TypeError](/web/js/err/type.md) ⟩ [assignment to constant variable❗️](/web/js/err/type/const-no-reassign.md)

```javascript
const { log } = console;

const array = [1,2,3];
const students = ['Joe', 'Jane', 'Jack'];

// ⭐️ for-in loop
//   ╭───╮ <------------------------- this is OK ✅
for (const index in students) {
    // this `const` behaves like:
    //
    //    // inside block scope
    //    const index = <next index in students>
    //
    log(`${index}: ${students[index]}`);
}

// ⭐️ for-of loop
//   ╭───╮ <------------------------- this is OK ✅
for (const value of array) {
    // this `const` behaves like:
    //
    //    // inside block scope
    //    const value = <next value of array>
    //
    log(value);
}

// ⭐️ classic for-loop❗
//
//   but this `const` behaves like: 
//
//       // in its own scope❗ (like function name scope)
//       // (inside for-loop but outside block scope)
//       // (between outer scope and inner block scope)
//
//       const i = 0;        // ⭐️ reassignment will fail❗
//
//  ╭─ for-loop init scope ─╮
//   ╭───╮ <------------------------- this is NOT OK ❌
for (const i = 0; i < 3; i++) {
    //                   ^^^
    // TypeError after the first iteration❗
    // (⛔ TypeError: Assignment to constant variable)
    log(i);
}

// output
// ---------------------
// 0: Joe
// 1: Jane
// 2: Jack
// 1
// 2
// 3
// 0 <----- ⭐️ first iteration (then fails)
// ⛔ TypeError: Assignment to constant variable.
```

{% 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. 5 ⟩ [Constants?](https://github.com/getify/You-Dont-Know-JS/blob/2nd-ed/scope-closures/ch5.md#constants)
  {% endtab %}
  {% endtabs %}
