--- id: javascript-switch title: "JavaScript Switch" category: "Frontend" status: "draft" verification_status: "conceptual" canonical_id: "" aliases: ["switch statement", "JS switch", "switch case", "case default break", "switch control flow"] 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", "switch", "control-flow", "conditionals"] raw_sources: ["https://www.w3schools.com/js/js_switch.asp"] applied_in: [] github_commit: "" --- # [[JavaScript Switch]] ## 🎯 ν•œ 쀄 톡찰 (One-line insight) The `switch` statement selects one of many code blocks to execute by comparing an expression against case values using strict comparison (`===`). [S1] ## 🧠 핡심 κ°œλ… (Core concepts) - **Selects a code block by condition** β€” based on a condition, `switch` selects one or more code blocks to be executed. [S1] - **`break` stops fall-through** β€” when JavaScript reaches `break`, it breaks out of the switch block, preventing unmatched cases from also executing. [S1] - **`default` is the fallback** β€” the `default` keyword specifies a block of code to run if there is no case match; it is optional. [S1] - **Strict comparison** β€” switch uses strict comparison (`===`); values must match in both value and type. [S1] - **First match wins** β€” if multiple cases match a case value, the first case is selected. [S1] ## 🧩 μΆ”μΆœλœ νŒ¨ν„΄ (Extracted patterns) - **Evaluate once, branch many** β€” the switch expression is evaluated once, then its value is compared against each case. [S1] - **Shared code blocks** β€” multiple cases can share a single code block by listing cases back-to-back before the shared code. [S1] - **Default-not-last requires `break`** β€” if `default` is not the last clause, terminate it with `break` so execution does not fall through. [S1] ## πŸ“– μ„ΈλΆ€ λ‚΄μš© (Details) **Switch control flow** [S1] Based on a condition, `switch` selects one or more code blocks to be executed. **Syntax** [S1] ```javascript switch(expression) { case x: // code block break; case y: // code block break; default: // code block } ``` **How it works** [S1] The switch expression is evaluated once. The value of the expression is compared with the values of each case. If there is a match, the associated block of code is executed. If there is no match, no execution occurs (unless a `default` is present). **Example β€” weekday calculator** [S1] The `getDay()` method returns the weekday as a number between 0 and 6 (Sunday=0, Monday=1, etc.). ```javascript switch (new Date().getDay()) { case 0: day = "Sunday"; break; case 1: day = "Monday"; break; case 2: day = "Tuesday"; break; case 3: day = "Wednesday"; break; case 4: day = "Thursday"; break; case 5: day = "Friday"; break; case 6: day = "Saturday"; } ``` **The `break` keyword** [S1] When JavaScript reaches a `break` keyword, it breaks out of the switch block. This stops the execution inside the switch block and prevents fall-through, where unmatched cases would otherwise also execute. **The `default` keyword** [S1] The `default` keyword specifies the code to run if there is no case match. ```javascript switch (new Date().getDay()) { case 6: text = "Today is Saturday"; break; case 0: text = "Today is Sunday"; break; default: text = "Looking forward to the Weekend"; } ``` **`default` not at the end** [S1] When `default` is not the last clause, terminate it with `break`. ```javascript switch (new Date().getDay()) { default: text = "Looking forward to the Weekend"; break; case 6: text = "Today is Saturday"; break; case 0: text = "Today is Sunday"; } ``` **Common code blocks** [S1] Sometimes you will want different switch cases to use the same code; multiple cases can share a code block. ```javascript switch (new Date().getDay()) { case 4: case 5: text = "Soon it is Weekend"; break; case 0: case 6: text = "It is Weekend"; break; default: text = "Looking forward to the Weekend"; } ``` **Switching details** [S1] If multiple cases match a case value, the first case is selected. If no matching cases are found, the program continues to the `default` label. If no default label is found, the program continues to the statements after the switch. **Strict comparison** [S1] Switch cases use strict comparison (`===`). The values must be of the same type to match. A strict comparison can only be true if the operands are of the same type. In this example there will be no match for `x`, because `x` is a string and the cases are numbers: ```javascript let x = "0"; switch (x) { case 0: text = "Off"; break; case 1: text = "On"; break; default: text = "No value found"; } ``` ## πŸ› οΈ 적용 사둀 (Applied in summary) The page's own snippets are the canonical applied examples β€” mapping `new Date().getDay()` to a weekday name, weekend detection via `default`, and grouping days with shared code blocks. No external project/commit applications found in the source. ## πŸ’» μ½”λ“œ νŒ¨ν„΄ (Code patterns) Basic switch with break and default: ```javascript switch(expression) { case x: // code block break; case y: // code block break; default: // code block } ``` Shared code block across cases: ```javascript switch (new Date().getDay()) { case 4: case 5: text = "Soon it is Weekend"; break; case 0: case 6: text = "It is Weekend"; break; default: text = "Looking forward to the Weekend"; } ``` ## βš–οΈ λͺ¨μˆœ 및 μ—…λ°μ΄νŠΈ (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 Else]], [[JavaScript Ternary]], [[JavaScript Break]], [[JavaScript Comparisons]] - **μ°Έμ‘° λ§₯락:** Chosen over long `if...else` chains when branching on the discrete values of a single expression. ## πŸ“š 좜처 (Sources) - [S1] W3Schools β€” JavaScript Switch β€” https://www.w3schools.com/js/js_switch.asp ## πŸ“ λ³€κ²½ 이λ ₯ (Change history) - 2026-06-23: Initial draft synthesized from the W3Schools "JavaScript Switch" page (Astra wiki-curation, P-Reinforce v3.1 format).