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,217 @@
|
||||
---
|
||||
id: javascript-booleans
|
||||
title: "JavaScript Booleans"
|
||||
category: "Frontend"
|
||||
status: "draft"
|
||||
verification_status: "conceptual"
|
||||
canonical_id: ""
|
||||
aliases: ["boolean", "true false", "JS booleans", "Boolean function", "truthy falsy"]
|
||||
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", "boolean", "truthy", "data-types"]
|
||||
raw_sources: ["https://www.w3schools.com/js/js_booleans.asp"]
|
||||
applied_in: []
|
||||
github_commit: ""
|
||||
---
|
||||
|
||||
# [[JavaScript Booleans]]
|
||||
|
||||
## 🎯 한 줄 통찰 (One-line insight)
|
||||
A Boolean is a primitive type with only two values — `true` or `false` — and the boolean value of an expression is the basis for all JavaScript comparisons and conditions. [S1]
|
||||
|
||||
## 🧠 핵심 개념 (Core concepts)
|
||||
- **Two values only** — a Boolean can only have one of two values: `true` or `false`, written in lowercase and without quotes. [S1]
|
||||
- **Comparisons return booleans** — all JavaScript comparison operators (`==`, `!=`, `<`, `>`) return `true` or `false`. [S1]
|
||||
- **Drives control flow** — booleans are used in `if` statements and loops to decide which code blocks run. [S1]
|
||||
- **`Boolean()` evaluates truthiness** — the `Boolean()` function reports whether an expression or variable is true. [S1]
|
||||
- **Truthy vs falsy** — everything with a "value" is true; everything without a "value" is false. [S1]
|
||||
- **Avoid Boolean objects** — booleans can be created as objects with `new Boolean()`, but this should not be done. [S1]
|
||||
|
||||
## 🧩 추출된 패턴 (Extracted patterns)
|
||||
- **Coerce-to-boolean check** — wrap an expression in `Boolean(...)` (or rely on an expression like `(10 > 9)`) to obtain its truth value. [S1]
|
||||
- **Truthiness rule** — empty arrays `[]` and empty objects `{}` are truthy because all objects evaluate to true; `0`, `""`, `undefined`, `null`, `NaN`, and `false` are falsy. [S1]
|
||||
- **Never compare a primitive boolean to a Boolean object** — `(x == y)` may be true while `(x === y)` is false, and two objects always compare as not equal. [S1]
|
||||
|
||||
## 📖 세부 내용 (Details)
|
||||
**The Boolean data type** [S1]
|
||||
In JavaScript, a Boolean is a primitive data type that can only have one of two values: `true` or `false`. The boolean value of an expression is the basis for all JavaScript comparisons and conditions. `true` and `false` are boolean data types, are the only possible boolean values, and must be written in lowercase and without quotes.
|
||||
|
||||
**Boolean use cases** [S1]
|
||||
Very often, in programming, you will need a data type that can represent one of two values, like: yes or no; on or off; true or false. Boolean values are fundamental for logical operations and control flow.
|
||||
|
||||
**Comparisons** [S1]
|
||||
All JavaScript comparison operators (like `==`, `!=`, `<`, `>`) return `true` or `false`. Given that `x = 5`:
|
||||
|
||||
| Description | Example | Returns |
|
||||
|---|---|---|
|
||||
| Equal to | (x == 8) | false |
|
||||
| Not equal to | (x != 8) | true |
|
||||
| Greater than | (x > 8) | false |
|
||||
| Less than | (x < 8) | true |
|
||||
|
||||
```javascript
|
||||
let x = 5;
|
||||
(x == 8); // equals false
|
||||
(x != 8); // equals true
|
||||
```
|
||||
|
||||
**Conditions** [S1]
|
||||
Booleans are extensively used in `if` statements to determine which code blocks to execute.
|
||||
|
||||
| Example | Result |
|
||||
|---|---|
|
||||
| if (day == "Monday") | true or false |
|
||||
| if (salary > 9000) | true or false |
|
||||
| if (age < 18) | true or false |
|
||||
|
||||
```javascript
|
||||
if (hour < 18) {
|
||||
greeting = "Good day";
|
||||
} else {
|
||||
greeting = "Good evening";
|
||||
}
|
||||
```
|
||||
|
||||
**Loops** [S1]
|
||||
Booleans are extensively used in loops to determine the looping condition.
|
||||
|
||||
| Description | Example |
|
||||
|---|---|
|
||||
| For loop | for (i = 0; i < 5; i++) |
|
||||
| While loop | while (i < 10) |
|
||||
| For in loop | for (x in person) |
|
||||
| For of loop | for (x of cars) |
|
||||
|
||||
```javascript
|
||||
while (i < 10) {
|
||||
text += i;
|
||||
i++;
|
||||
}
|
||||
```
|
||||
|
||||
**The `Boolean()` function** [S1]
|
||||
You can use the `Boolean()` function to find out if an expression (or a variable) is true:
|
||||
```javascript
|
||||
Boolean(10 > 9)
|
||||
```
|
||||
Or even easier:
|
||||
```javascript
|
||||
(10 > 9)
|
||||
```
|
||||
|
||||
**Everything with a "value" is true** [S1]
|
||||
The following all evaluate to true: `100`, `3.14`, `-15`, `true`, `"Hello"`, `"false"`, `(7 + 1 + 3.14)`, `[ ]`, `{ }`.
|
||||
|
||||
Note: In JavaScript, both an empty array `[ ]` and an empty object `{ }` are truthy because they are objects. All objects in JavaScript evaluate to true in a boolean context, regardless of their content.
|
||||
|
||||
**Everything without a "value" is false** [S1]
|
||||
The following all evaluate to false: `0`, `""`, `undefined`, `null`, `NaN`, `false`.
|
||||
|
||||
The boolean value of `0` (zero) is false:
|
||||
```javascript
|
||||
let x = 0;
|
||||
Boolean(x);
|
||||
```
|
||||
The boolean value of `-0` (minus zero) is false:
|
||||
```javascript
|
||||
let x = -0;
|
||||
Boolean(x);
|
||||
```
|
||||
The boolean value of `""` (empty string) is false:
|
||||
```javascript
|
||||
let x = "";
|
||||
Boolean(x);
|
||||
```
|
||||
The boolean value of `undefined` is false:
|
||||
```javascript
|
||||
let x;
|
||||
Boolean(x);
|
||||
```
|
||||
The boolean value of `null` is false:
|
||||
```javascript
|
||||
let x = null;
|
||||
Boolean(x);
|
||||
```
|
||||
The boolean value of `false` is false:
|
||||
```javascript
|
||||
let x = false;
|
||||
Boolean(x);
|
||||
```
|
||||
The boolean value of `NaN` is false:
|
||||
```javascript
|
||||
let x = 10 / "Hallo";
|
||||
Boolean(x);
|
||||
```
|
||||
|
||||
**JavaScript booleans as objects** [S1]
|
||||
Normally JavaScript booleans are primitive values created from literals:
|
||||
```javascript
|
||||
let x = false;
|
||||
```
|
||||
But booleans can also be defined as objects with the keyword `new`:
|
||||
```javascript
|
||||
let y = new Boolean(false);
|
||||
```
|
||||
```javascript
|
||||
let x = false;
|
||||
let y = new Boolean(false);
|
||||
|
||||
// typeof x returns boolean
|
||||
// typeof y returns object
|
||||
```
|
||||
|
||||
Warning: Do not create Boolean objects. The `new` keyword complicates the code and slows down execution speed. Boolean objects can produce unexpected results.
|
||||
|
||||
Booleans and boolean objects cannot be safely compared:
|
||||
```javascript
|
||||
let x = Boolean(false);
|
||||
let y = new Boolean(false);
|
||||
|
||||
// (x == y) returns true
|
||||
// (x === y) returns false
|
||||
```
|
||||
Comparing two JavaScript objects always returns false.
|
||||
|
||||
## 🛠️ 적용 사례 (Applied in summary)
|
||||
The page's own snippets are the canonical applied examples — comparison results, an `if/else` greeting, a `while` accumulation loop, and `Boolean()` truthiness checks across falsy values. No external project/commit applications found in the source.
|
||||
|
||||
## 💻 코드 패턴 (Code patterns)
|
||||
Check truthiness of an expression:
|
||||
```javascript
|
||||
Boolean(10 > 9)
|
||||
```
|
||||
Detect a falsy value:
|
||||
```javascript
|
||||
let x = "";
|
||||
Boolean(x); // false
|
||||
```
|
||||
Avoid Boolean objects (anti-pattern noted by source):
|
||||
```javascript
|
||||
let y = new Boolean(false); // typeof y returns object
|
||||
```
|
||||
|
||||
## ⚖️ 모순 및 업데이트 (Contradictions & updates)
|
||||
No contradictions found in the source. The source explicitly warns against the `new Boolean()` object form as an anti-pattern.
|
||||
|
||||
## ✅ 검증 상태 및 신뢰도
|
||||
- **상태:** draft
|
||||
- **검증 단계:** conceptual (실제 적용 사례 발견 시 applied/validated로 승격 가능)
|
||||
- **출처 신뢰도:** B (W3Schools — widely used educational reference, not a primary standards body)
|
||||
- **신뢰 점수:** 0.89
|
||||
- **중복 검사 결과:** 신규 생성 (New discovery)
|
||||
|
||||
## 🔗 지식 그래프 (Knowledge Graph)
|
||||
- **상위/루트:** [[JavaScript Tutorial]]
|
||||
- **관련 개념:** [[JavaScript Comparisons]], [[JavaScript If Else]], [[JavaScript Logical]], [[JavaScript Data Types]]
|
||||
- **참조 맥락:** Underlies every condition and comparison; central to truthy/falsy reasoning in conditionals and loops.
|
||||
|
||||
## 📚 출처 (Sources)
|
||||
- [S1] W3Schools — JavaScript Booleans — https://www.w3schools.com/js/js_booleans.asp
|
||||
|
||||
## 📝 변경 이력 (Change history)
|
||||
- 2026-06-23: Initial draft synthesized from the W3Schools "JavaScript Booleans" page (Astra wiki-curation, P-Reinforce v3.1 format).
|
||||
Reference in New Issue
Block a user