--- id: javascript-while-loop title: "JavaScript While Loop" category: "Frontend" status: "draft" verification_status: "conceptual" canonical_id: "" aliases: ["while loop", "do while", "JS while loop", "condition loop", "do while loop"] 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", "while-loop", "do-while", "loops", "iteration"] raw_sources: ["https://www.w3schools.com/js/js_loop_while.asp"] applied_in: [] github_commit: "" --- # [[JavaScript While Loop]] ## 🎯 ν•œ 쀄 톡찰 (One-line insight) A `while` loop runs a block of code as long as a condition is true; its `do/while` variant guarantees the block runs at least once before the condition is checked. [S1] ## 🧠 핡심 κ°œλ… (Core concepts) - **Condition-driven repetition** β€” the `while` loop loops through a block of code as long as a specified condition is true. [S1] - **Two while forms** β€” JavaScript offers the `while` loop and the `do/while` loop. [S1] - **`do/while` runs once first** β€” it executes the code block once before checking the condition, then repeats while the condition is true. [S1] - **Runs at least once** β€” `do/while` runs at least once, even if the condition is false from the start. [S1] - **Infinite-loop hazard** β€” forgetting to increase the condition variable means the loop never ends and crashes the browser. [S1] ## 🧩 μΆ”μΆœλœ νŒ¨ν„΄ (Extracted patterns) - **Advance the counter inside the body** β€” increment the loop variable (`i++`) within the block so the condition eventually fails. [S1] - **Truthy-element traversal** β€” loop while `cars[i]` is truthy to walk an array until it runs out of elements. [S1] - **`for` ↔ `while` equivalence** β€” a `for (;cars[i];)` loop and a `while (cars[i])` loop express the same iteration. [S1] ## πŸ“– μ„ΈλΆ€ λ‚΄μš© (Details) **Introduction** [S1] While loops execute a block of code as long as a specified condition is true. JavaScript offers two while loop types: the `while` loop and the `do/while` loop. **The while loop** [S1] The `while` loop loops through a block of code as long as a specified condition is true. ```javascript while (condition) { // code block to be executed } ``` ```javascript while (i < 10) { text += "The number is " + i; i++; } ``` Warning: 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 is a variant of the while loop. This loop will execute the code block once, before checking if the condition is true, then it will repeat the loop as long as the condition is true. ```javascript do { // code block to be executed } while (condition); ``` ```javascript do { text += "The number is " + i; i++; } while (i < 10); ``` The `do while` runs at least once, even if the condition is false from the start. **Comparing for and while** [S1] Using a `for` loop to traverse a cars array: ```javascript const cars = ["BMW", "Volvo", "Saab", "Ford"]; let i = 0; let text = ""; for (;cars[i];) { text += cars[i]; i++; } ``` The same loop expressed as a `while` loop: ```javascript const cars = ["BMW", "Volvo", "Saab", "Ford"]; let i = 0; let text = ""; while (cars[i]) { text += cars[i]; i++; } ``` ## πŸ› οΈ 적용 사둀 (Applied in summary) The page's own snippets are the canonical applied examples β€” accumulating numbers while `i < 10` in both `while` and `do/while` forms, and traversing a `cars` array while `cars[i]` is truthy. No external project/commit applications found in the source. ## πŸ’» μ½”λ“œ νŒ¨ν„΄ (Code patterns) Standard while loop: ```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); ``` Truthy-element array traversal: ```javascript while (cars[i]) { text += cars[i]; i++; } ``` ## βš–οΈ 비ꡐ 및 선택 κΈ°μ€€ (Comparison & decision criteria) - **`while` vs `do/while`** β€” use `do/while` when the block must run at least once regardless of the initial condition; use `while` when the block should be skipped entirely if the condition is false at the start. [S1] - **`for` vs `while`** β€” the two are interchangeable for condition-only iteration (`for (;cars[i];)` equals `while (cars[i])`); choose based on whether you want the init/update folded into the header. [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 For Loop]], [[JavaScript Break]], [[JavaScript Booleans]] - **μ°Έμ‘° λ§₯락:** The condition-driven iteration construct; companion to the counted [[JavaScript For Loop]]. ## πŸ“š 좜처 (Sources) - [S1] W3Schools β€” JavaScript While Loop β€” https://www.w3schools.com/js/js_loop_while.asp ## πŸ“ λ³€κ²½ 이λ ₯ (Change history) - 2026-06-23: Initial draft synthesized from the W3Schools "JavaScript While Loop" page (Astra wiki-curation, P-Reinforce v3.1 format).