docs(10_Wiki): W3Schools 위키화 — HTML/CSS/JavaScript(core)
W3Schools 튜토리얼을 P-Reinforce v3.1 포맷으로 위키화(영어 본문, 한/영 섹션 헤더). - Topic_HTML: 59문서 (튜토리얼+예제, 레퍼런스/메타 제외) - Topic_CSS: 190문서 (메인 + Advanced/Flexbox/Grid/RWD 전체) - Topic_JavaScript: 120문서 (코어 언어; Temporal/DOM상세/BOM/WebAPI/AJAX/jQuery/Graphics 등은 후속) 각 폴더 00_INDEX.md(MOC) 포함. 코드 verbatim, 미확인분은 "Not found in source" 표기. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,208 @@
|
||||
---
|
||||
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).
|
||||
Reference in New Issue
Block a user