--- 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).