---
id: javascript-for-loop
title: "JavaScript For Loop"
category: "Frontend"
status: "draft"
verification_status: "conceptual"
canonical_id: ""
aliases: ["for loop", "JS for loop", "for statement", "loop expressions", "for loop scope"]
duplicate_of: ""
source_trust_level: "B"
confidence_score: 0.89
created_at: 2026-06-23
updated_at: 2026-06-23
review_reason: ""
merge_history: []
tags: ["javascript", "js", "web", "frontend", "w3schools", "for-loop", "loops", "iteration"]
raw_sources: ["https://www.w3schools.com/js/js_loop_for.asp"]
applied_in: []
github_commit: ""
---
# [[JavaScript For Loop]]
## π― ν μ€ ν΅μ°° (One-line insight)
The `for` statement creates a loop with three optional expressions β initialize, test, update β letting a code block run a controlled number of times. [S1]
## π§ ν΅μ¬ κ°λ
(Core concepts)
- **Three optional expressions** β the `for` statement creates a loop with three optional expressions. [S1]
- **exp1 β initializer** β sets a variable before the loop starts (`let i = 0`), and can be omitted if initialization happens beforehand. [S1]
- **exp2 β condition** β defines the condition for the loop to run; it is optional, but if omitted you must `break` inside the loop or it runs forever. [S1]
- **exp3 β update** β increases a value (`i++`) after the code block executes; it is optional and can do other operations. [S1]
- **Loop scope matters** β `let` scopes the counter to the loop; `var` redeclares the outer variable. [S1]
## π§© μΆμΆλ ν¨ν΄ (Extracted patterns)
- **Index-driven array traversal** β cache `cars.length` once, then iterate `i` from 0 to that length. [S1]
- **Omit-and-supply elsewhere** β any of the three expressions can be moved out of the `for(...)` header (initialize before, update inside the body). [S1]
- **Prefer `let` for the counter** β keeps `i` private to the loop. [S1]
## π μΈλΆ λ΄μ© (Details)
**The `for` statement** [S1]
The `for` statement creates a loop with 3 optional expressions, allowing the code block to execute multiple times.
- **exp1** sets a variable before the loop starts (`let i = 0`).
- **exp2** defines the condition for the loop to run (`i` must be less than 5).
- **exp3** increases a value (`i++`) after the code block has been executed.
The first expression can be omitted if initialization occurs before the loop. The second expression is optional but requires a `break` statement inside the loop to prevent infinite execution. The third expression is optional and can perform various operations like negative increment or custom calculations.
**Basic for loop** [S1]
```javascript
for (let i = 0; i < 5; i++) {
text += "The number is " + i + "
";
}
```
**Iterating through an array** [S1]
```javascript
const cars = ["BMW", "Volvo", "Saab", "Ford"];
let len = cars.length;
let text = "";
for (let i = 0; i < len; i++) {
text += cars[i];
}
```
**Omitting exp1** [S1]
```javascript
const cars = ["BMW", "Volvo", "Saab", "Ford"];
let len = cars.length;
let i = 2;
let text = "";
for (; i < len; i++) {
text += cars[i] + "
";
}
```
**Omitting exp3** [S1]
```javascript
const cars = ["BMW", "Volvo", "Saab", "Ford"];
let len = cars.length;
let i = 0;
let text = "";
for (; i < len; ) {
text += cars[i] + "
";
i++;
}
```
**Loop scope** [S1]
Using `var` in a loop, the loop variable redeclares the outer variable:
```javascript
var i = 5;
for (var i = 0; i < 10; i++) {
// some code
}
// Here i is 10
```
Using `let` in a loop, the variable is scoped only within the loop. When `let` is used to declare the `i` variable, the `i` variable will only be visible within the loop:
```javascript
let i = 5;
for (let i = 0; i < 10; i++) {
// some code
}
// Here i is 5
```
## π οΈ μ μ© μ¬λ‘ (Applied in summary)
The page's own snippets are the canonical applied examples β counting 0β4, traversing a `cars` array (with `len` cached), and demonstrating exp1/exp3 omission and `let`/`var` scoping. No external project/commit applications found in the source.
## π» μ½λ ν¨ν΄ (Code patterns)
Standard counted loop:
```javascript
for (let i = 0; i < 5; i++) {
text += "The number is " + i + "
";
}
```
Array traversal with cached length:
```javascript
const cars = ["BMW", "Volvo", "Saab", "Ford"];
let len = cars.length;
for (let i = 0; i < len; i++) {
text += cars[i];
}
```
## βοΈ λΉκ΅ λ° μ ν κΈ°μ€ (Comparison & decision criteria)
- **`let` vs `var` for the counter** β `let` keeps `i` visible only inside the loop (`i` is 5 afterward in the example); `var` leaks/redeclares it to the outer scope (`i` is 10 afterward). Prefer `let`. [S1]
## βοΈ λͺ¨μ λ° μ
λ°μ΄νΈ (Contradictions & updates)
No contradictions found in the source.
## β
κ²μ¦ μν λ° μ λ’°λ
- **μν:** draft
- **κ²μ¦ λ¨κ³:** conceptual (μ€μ μ μ© μ¬λ‘ λ°κ²¬ μ applied/validatedλ‘ μΉκ²© κ°λ₯)
- **μΆμ² μ λ’°λ:** B (W3Schools β widely used educational reference, not a primary standards body)
- **μ λ’° μ μ:** 0.89
- **μ€λ³΅ κ²μ¬ κ²°κ³Ό:** μ κ· μμ± (New discovery)
## π μ§μ κ·Έλν (Knowledge Graph)
- **μμ/루νΈ:** [[JavaScript Tutorial]]
- **κ΄λ ¨ κ°λ
:** [[JavaScript Loops]], [[JavaScript While Loop]], [[JavaScript Break]], [[JavaScript For In]]
- **μ°Έμ‘° λ§₯λ½:** The primary counted-iteration construct; detailed expansion of the `for` loop introduced in [[JavaScript Loops]].
## π μΆμ² (Sources)
- [S1] W3Schools β JavaScript For Loop β https://www.w3schools.com/js/js_loop_for.asp
## π λ³κ²½ μ΄λ ₯ (Change history)
- 2026-06-23: Initial draft synthesized from the W3Schools "JavaScript For Loop" page (Astra wiki-curation, P-Reinforce v3.1 format).