--- id: javascript-control-flow title: "JavaScript Control Flow" category: "Frontend" status: "draft" verification_status: "conceptual" canonical_id: "" aliases: ["control flow", "program flow", "execution order", "JS control flow", "conditional flow"] 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", "control-flow", "loops", "conditions"] raw_sources: ["https://www.w3schools.com/js/js_control_flow.asp"] applied_in: [] github_commit: "" --- # [[JavaScript Control Flow]] ## 🎯 ν•œ 쀄 톡찰 (One-line insight) Control flow is the order in which statements are executed; by default JavaScript runs top-to-bottom and left-to-right on a single thread, and conditions, loops, jumps, and functions are how you alter that order. [S1] ## 🧠 핡심 κ°œλ… (Core concepts) - **Control flow = order of execution** β€” control flow is the order in which statements are executed in a program. [S1] - **Default flow is sequential** β€” by default JavaScript executes code sequentially from top to bottom and left to right. [S1] - **Conditional flow** β€” conditions allow decision-making using `if`, `if...else`, `switch`, and the ternary (`? :`) operators. [S1] - **Loops repeat code** β€” loops enable code to run multiple times using `for`, `while`, or `do...while` structures. [S1] - **Jump statements alter flow abruptly** β€” using `break`, `continue`, `return`, and `throw`. [S1] - **Functions are callable, reusable blocks** β€” functions run when they are called. [S1] - **Single-threaded** β€” JavaScript runs on a single thread; it can only do one thing at a time, so every task waits for the completion of previous tasks. [S1] ## 🧩 μΆ”μΆœλœ νŒ¨ν„΄ (Extracted patterns) - **Branch on a condition** β€” assign a default value, then override it inside `if/else` based on a test (e.g. `age >= 18`). [S1] - **Repeat with a counted loop** β€” drive repetition with a `for` loop counter and accumulate output. [S1] - **Early exit from a loop** β€” combine a loop with a `break` inside an `if` to stop early. [S1] - **Encapsulate logic in a function** β€” wrap reusable computation in a function that returns a value. [S1] ## πŸ“– μ„ΈλΆ€ λ‚΄μš© (Details) **JavaScript Control Flow** Control flow is the order in which statements are executed in a program. By default, JavaScript executes code sequentially from top to bottom and left to right. Control flow statements enable developers to alter this sequence based on conditions, loops, or keywords. [S1] **Default Flow** Default flow runs code sequentially from top to bottom and left to right. [S1] ```javascript let x = 5; let y = 6; let z = x + y; ``` **Conditional Control Flow** Conditions allow decision-making using `if`, `if...else`, `switch`, and ternary (`? :`) operators. [S1] ```javascript let text = "Unknown"; if (age >= 18) { text = "Adult"; } else { text = "Minor"; } ``` **Loops (Repetition Control Flow)** Loops enable code to run multiple times using `for`, `while`, or `do...while` structures. [S1] ```javascript for (let i = 0; i < 5; i++) { text += "The number is " + i + "
"; } ``` **Jump Statements** Jump statements alter flow abruptly using `break`, `continue`, `return`, and `throw`. [S1] ```javascript for (let i = 0; i < 10; i++) { if (i === 3) { break; } text += "The number is " + i + "
"; } ``` **Function Flow** Functions are callable and reusable code blocks. Functions will run when they are called. [S1] ```javascript function myFunction(p1, p2) { return p1 * p2; } ``` **JavaScript Is Single-Threaded** JavaScript runs on a single thread. It can only do one thing at a time. Every task must wait for completion of previous tasks, potentially freezing applications during slow operations. Asynchronous programming is covered in the advanced section. [S1] ## πŸ› οΈ 적용 사둀 (Applied in summary) The page's own snippets are the canonical applied examples β€” sequential assignment, an `if/else` age check, a counted `for` loop, an early `break`, and a multiplying function. No external project/commit applications found in the source. ## πŸ’» μ½”λ“œ νŒ¨ν„΄ (Code patterns) Conditional branch: ```javascript if (age >= 18) { text = "Adult"; } else { text = "Minor"; } ``` Counted loop with early exit: ```javascript for (let i = 0; i < 10; i++) { if (i === 3) { break; } text += "The number is " + i + "
"; } ``` Reusable function: ```javascript function myFunction(p1, p2) { return p1 * p2; } ``` ## βš–οΈ λͺ¨μˆœ 및 μ—…λ°μ΄νŠΈ (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 If Else]], [[JavaScript Switch]], [[JavaScript For Loop]], [[JavaScript Break]], [[JavaScript Continue]], [[JavaScript Functions]] - **μ°Έμ‘° λ§₯락:** The conceptual umbrella for every statement that changes execution order in a JavaScript program. ## πŸ“š 좜처 (Sources) - [S1] W3Schools β€” JavaScript Control Flow β€” https://www.w3schools.com/js/js_control_flow.asp ## πŸ“ λ³€κ²½ 이λ ₯ (Change history) - 2026-06-23: Initial draft synthesized from the W3Schools "JavaScript Control Flow" page (Astra wiki-curation, P-Reinforce v3.1 format).