---
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).