--- id: javascript-loops title: "JavaScript Loops" category: "Frontend" status: "draft" verification_status: "conceptual" canonical_id: "" aliases: ["loops", "JS loops", "for loop", "while loop", "do while", "iteration"] duplicate_of: "" source_trust_level: "B" confidence_score: 0.88 created_at: 2026-06-23 updated_at: 2026-06-23 review_reason: "" merge_history: [] tags: ["javascript", "js", "web", "frontend", "w3schools", "loops", "iteration", "control-flow"] raw_sources: ["https://www.w3schools.com/js/js_loops.asp"] applied_in: [] github_commit: "" --- # [[JavaScript Loops]] ## 🎯 ν•œ 쀄 톡찰 (One-line insight) Loops let you run the same block of code many times, each time with a different value β€” replacing long repetitive sequences with a compact iteration. [S1] ## 🧠 핡심 κ°œλ… (Core concepts) - **Loops repeat a block of code** β€” they execute a code block multiple times, often with a different value each pass. [S1] - **`for` loop** β€” uses three expressions: an initializer, a condition, and an update run after each iteration. [S1] - **`while` loop** β€” runs as long as a specified condition is true. [S1] - **`do/while` loop** β€” a variant that runs the block at least once before checking the condition. [S1] - **Loop scope** β€” variables declared with `let` inside a loop are visible only within the loop, unlike `var`. [S1] - **Infinite-loop hazard** β€” forgetting to update the condition variable means the loop never ends and crashes the browser. [S1] ## 🧩 μΆ”μΆœλœ νŒ¨ν„΄ (Extracted patterns) - **Replace repetition with iteration** β€” instead of writing the same statement for each array index, loop over `cars.length`. [S1] - **Index accumulation** β€” append each iteration's value into a result string (`text += ...`). [S1] - **Always advance the condition variable** β€” increment (e.g. `i++`) so the condition eventually becomes false. [S1] ## πŸ“– μ„ΈλΆ€ λ‚΄μš© (Details) **Why loops** [S1] Loops are handy if you want to run the same code over and over again, each time with a different value. Instead of writing the same statement many times: ```javascript text += cars[0] + "
"; text += cars[1] + "
"; text += cars[2] + "
"; text += cars[3] + "
"; text += cars[4] + "
"; text += cars[5] + "
"; ``` You can write: ```javascript for (let i = 0; i < cars.length; i++) { text += cars[i] + "
"; } ``` **The `for` loop** [S1] The `for` loop has the syntax `for (expr1; expr2; expr3) { // code block to be executed }`. ```javascript for (let i = 0; i < 5; i++) { text += "The number is " + i + "
"; } ``` **Loop scope β€” `var` vs `let`** [S1] Using `var` in a loop, the loop variable redeclares the outer variable: ```javascript let i = 5; for (i = 0; i < 10; i++) { // some code } // Here i is 10 ``` Using `let` in a loop, the loop variable is scoped only to the loop: ```javascript let i = 5; for (let i = 0; i < 10; i++) { // some code } // Here i is 5 ``` **The `while` loop** [S1] The `while` loop has the syntax `while (condition) { // code block to be executed }`. ```javascript while (i < 10) { text += "The number is " + i; i++; } ``` Note: If you forget to increase the variable used in the condition, the loop will never end. This will crash your browser. **The `do/while` loop** [S1] The `do/while` loop has the syntax `do { // code block to be executed } while (condition);`. ```javascript do { text += "The number is " + i; i++; } while (i < 10); ``` Note: The `do while` runs at least once, even if the condition is false from the start. **Different kinds of loops** [S1] JavaScript supports the `for` loop (loops through a block of code a number of times), the `for/in` loop (loops through the properties of an object), the `for/of` loop (loops through the values of an iterable object), the `while` loop (loops through a block of code while a condition is true), and the `do/while` loop (also loops while a condition is true). ## πŸ› οΈ 적용 사둀 (Applied in summary) The page's own snippets are the canonical applied examples β€” replacing repeated `text += cars[n]` statements with a `for` loop over `cars.length`, and accumulating numbers in `for`/`while`/`do-while` loops. No external project/commit applications found in the source. ## πŸ’» μ½”λ“œ νŒ¨ν„΄ (Code patterns) Iterate an array: ```javascript for (let i = 0; i < cars.length; i++) { text += cars[i] + "
"; } ``` While loop with advancing counter: ```javascript while (i < 10) { text += "The number is " + i; i++; } ``` Do-while (runs at least once): ```javascript do { text += "The number is " + i; i++; } while (i < 10); ``` ## βš–οΈ 비ꡐ 및 선택 κΈ°μ€€ (Comparison & decision criteria) - **`for` vs `while`** β€” use `for` when the number of iterations / the counter is known up front (init, condition, update in one place); use `while` when looping purely on a condition. [S1] - **`while` vs `do/while`** β€” use `do/while` when the block must run at least once even if the condition is initially false. [S1] - **`let` vs `var` for the loop variable** β€” `let` keeps the counter scoped to the loop; `var` leaks it to the surrounding scope. [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.88 - **쀑볡 검사 κ²°κ³Ό:** μ‹ κ·œ 생성 (New discovery) ## πŸ”— 지식 κ·Έλž˜ν”„ (Knowledge Graph) - **μƒμœ„/루트:** [[JavaScript Tutorial]] - **κ΄€λ ¨ κ°œλ…:** [[JavaScript For Loop]], [[JavaScript While Loop]], [[JavaScript Break]], [[JavaScript Booleans]] - **μ°Έμ‘° λ§₯락:** The overview entry point for all iteration constructs; specific loop types are detailed in their own pages. ## πŸ“š 좜처 (Sources) - [S1] W3Schools β€” JavaScript Loops β€” https://www.w3schools.com/js/js_loops.asp ## πŸ“ λ³€κ²½ 이λ ₯ (Change history) - 2026-06-23: Initial draft synthesized from the W3Schools "JavaScript Loops" page (Astra wiki-curation, P-Reinforce v3.1 format).