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