--- id: javascript-if-else title: "JavaScript If Else" category: "Frontend" status: "draft" verification_status: "conceptual" canonical_id: "" aliases: ["JS if else", "else statement", "else if statement", "if else if chain", "if else syntax", "branching"] 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", "if-else", "control-flow"] raw_sources: ["https://www.w3schools.com/js/js_if_else.asp"] applied_in: [] github_commit: "" --- # [[JavaScript If Else]] ## 🎯 ν•œ 쀄 톡찰 (One-line insight) Use `else` to run a block when the condition is `false`, and `else if` to test a new condition when the first is `false`, forming a multi-branch decision chain. [S1] ## 🧠 핡심 κ°œλ… (Core concepts) - **`else` handles the false case** β€” use the `else` statement to specify a block of code to be executed if a condition is `false`. [S1] - **`else if` chains a new test** β€” use the `else if` statement to specify a new condition if the first is `false`. [S1] - **First-true-wins** β€” in an `if / else if / else` chain, the first branch whose condition is `true` runs, and the trailing `else` is the fallback when all conditions are `false`. [S1] - **Builds on `if`** β€” `else` and `else if` extend the basic `if` statement rather than replacing it. [S1] ## 🧩 μΆ”μΆœλœ νŒ¨ν„΄ (Extracted patterns) - **Two-way branch** β€” `if (...) { ... } else { ... }` to pick exactly one of two blocks. [S1] - **Multi-way ladder** β€” chain `else if` for graduated thresholds (e.g. time-of-day greeting). [S1] - **Branch then render** β€” compute a value in branches, then write it to the DOM (`innerHTML`). [S1] ## πŸ“– μ„ΈλΆ€ λ‚΄μš© (Details) **The else Statement** [S1] Use the `else` statement to specify a block of code to be executed if a condition is `false`. Syntax: [S1] ``` if (condition) { // block of code to be executed if the condition is true } else { // block of code to be executed if the condition is false } ``` Example β€” time-of-day greeting: [S1] ```javascript if (hour < 18) { greeting = "Good day"; } else { greeting = "Good evening"; } ``` **The else if Statement** [S1] Use the `else if` statement to specify a new condition if the first is `false`. Syntax: [S1] ``` if (condition1) { // block of code to be executed if condition1 is true } else if (condition2) { // block of code to be executed if the condition1 is false and condition2 is true } else { // block of code to be executed if the condition1 is false and condition2 is false } ``` Example β€” three-way greeting: [S1] ```javascript if (time < 10) { greeting = "Good morning"; } else if (time < 20) { greeting = "Good day"; } else { greeting = "Good evening"; } ``` Example β€” branch then render to the page: [S1] ```javascript let text; if (Math.random() < 0.5) { text = "Visit W3Schools"; } else { text = "Visit WWF"; } document.getElementById("demo").innerHTML = text; ``` ## πŸ› οΈ 적용 사둀 (Applied in summary) The page's own snippets are the canonical applied examples β€” a two-way greeting, a three-way `else if` greeting ladder, and a random-link example that branches then writes the result into `#demo` via `innerHTML`. No external project/commit applications found in the source. ## πŸ’» μ½”λ“œ νŒ¨ν„΄ (Code patterns) Two-way branch: ```javascript if (hour < 18) { greeting = "Good day"; } else { greeting = "Good evening"; } ``` Multi-way ladder with `else if`: ```javascript if (time < 10) { greeting = "Good morning"; } else if (time < 20) { greeting = "Good day"; } else { greeting = "Good evening"; } ``` ## βš–οΈ λͺ¨μˆœ 및 μ—…λ°μ΄νŠΈ (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 If]], [[JavaScript Conditional Operators]], [[JavaScript Comparisons]], [[JavaScript Introduction]] - **μ°Έμ‘° λ§₯락:** Referenced whenever a decision needs more than a single true-only branch. ## πŸ“š 좜처 (Sources) - [S1] W3Schools β€” JavaScript If Else β€” https://www.w3schools.com/js/js_if_else.asp ## πŸ“ λ³€κ²½ 이λ ₯ (Change history) - 2026-06-23: Initial draft synthesized from the W3Schools "JavaScript If Else" page (Astra wiki-curation, P-Reinforce v3.1 format).