--- id: javascript-logical-operators title: "JavaScript Logical Operators" category: "Frontend" status: "draft" verification_status: "conceptual" canonical_id: "" aliases: ["logical operators", "AND OR NOT", "&& || !", "nullish coalescing", "JS logic"] 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", "logical-operators", "nullish-coalescing", "operators"] raw_sources: ["https://www.w3schools.com/js/js_logical.asp"] applied_in: [] github_commit: "" --- # [[JavaScript Logical Operators]] ## 🎯 ν•œ 쀄 톡찰 (One-line insight) Logical operators (`&&`, `||`, `!`) combine boolean expressions into more complex logic, while `??` returns a fallback only when the left operand is nullish (`null` or `undefined`). [S1] ## 🧠 핡심 κ°œλ… (Core concepts) - **Combine boolean expressions** β€” logical operators are used to combine boolean expressions and to modify the results of comparisons. [S1] - **`&&` (AND)** β€” true only when both operands are true. [S1] - **`||` (OR)** β€” true when at least one operand is true. [S1] - **`!` (NOT)** β€” inverts a boolean result. [S1] - **`??` (Nullish coalescing)** β€” returns the right operand when the left operand is nullish (`null` or `undefined`); otherwise returns the left operand. [S1] - **Nullish β‰  falsy** β€” `??` checks specifically for `null`/`undefined`, so values like `0`, `""`, and `false` are preserved. [S1] ## 🧩 μΆ”μΆœλœ νŒ¨ν„΄ (Extracted patterns) - **Condition + combinator** β€” use a comparison operator to check a condition and a logical operator to combine conditions into more complex logic. [S1] - **Nullish-safe default** β€” use `??` when an empty string or `false` is an acceptable value and only `null`/`undefined` should trigger the fallback. [S1] ## πŸ“– μ„ΈλΆ€ λ‚΄μš© (Details) **Logical operators** [S1] Logical operators are used to combine boolean expressions. Logical operators can be used to modify the results of comparisons. Typically, you will use a comparison operator to check a condition, and a logical operator to combine conditions into more complex logic. Logical operators are used to determine the logic between variables or values. Given `x = 6` and `y = 3`: | Operator | Name | Example | |----------|------|---------| | && | AND | (x < 10 && y > 1) is true | | \|\| | OR | (x === 5 \|\| y === 5) is false | | ! | NOT | !(x === y) is true | **AND (`&&`) example** [S1] ```javascript let x = 6; let y = 3; let z = (x < 10 && y > 1) ``` **OR (`||`) example** [S1] ```javascript let x = 6; let y = -3; let z = (x > 0 || y > 0) ``` **NOT (`!`) example** [S1] ```javascript let x = (5 == 8); let y = !(5 == 8) ``` **The Nullish Coalescing Operator (`??`)** [S1] The `??` operator returns the right operand when the left operand is nullish (`null` or `undefined`), otherwise it returns the left operand. ```javascript let name = null; let text = "missing"; let result = name ?? text; ``` When programming, a lot of values can be falsy (like `0`, empty strings, `false`, `undefined`, `null`, `NaN`). However, sometimes you want to check if a variable is nullish (either `undefined` or `null`), like when it is okay for a variable to be an empty string or a false value. Then you can use the nullish coalescing operator. **Browser support** [S1] `??` is an ES2020 feature. ES2020 is fully supported in all modern browsers since September 2020 (Chrome 85, Edge 85, Firefox 79, Safari 14, Opera 71). ## πŸ› οΈ 적용 사둀 (Applied in summary) The page's own snippets are the canonical applied examples β€” combining comparisons with `&&` and `||`, negating with `!`, and supplying a nullish-safe default with `??`. No external project/commit applications found in the source. ## πŸ’» μ½”λ“œ νŒ¨ν„΄ (Code patterns) Combine conditions with AND / OR: ```javascript let z = (x < 10 && y > 1) let w = (x > 0 || y > 0) ``` Negate a result: ```javascript let y = !(5 == 8) ``` Nullish-safe default: ```javascript let name = null; let text = "missing"; let result = name ?? text; ``` ## βš–οΈ 비ꡐ 및 선택 κΈ°μ€€ (Comparison & decision criteria) - **`??` vs `||` for defaults** β€” `||` falls back on any falsy value (`0`, `""`, `false`, `null`, `undefined`, `NaN`), whereas `??` falls back only on nullish values (`null`/`undefined`). Choose `??` when an empty string or `false` is a valid value that should be preserved. [S1] ## βš–οΈ λͺ¨μˆœ 및 μ—…λ°μ΄νŠΈ (Contradictions & updates) No contradictions found in the source. The `??` operator is noted as an ES2020 addition relative to the older `&&`/`||`/`!` operators. ## βœ… 검증 μƒνƒœ 및 신뒰도 - **μƒνƒœ:** draft - **검증 단계:** conceptual (μ‹€μ œ 적용 사둀 발견 μ‹œ applied/validated둜 승격 κ°€λŠ₯) - **좜처 신뒰도:** B (W3Schools β€” widely used educational reference, not a primary standards body) - **μ‹ λ’° 점수:** 0.89 - **쀑볡 검사 κ²°κ³Ό:** μ‹ κ·œ 생성 (New discovery) ## πŸ”— 지식 κ·Έλž˜ν”„ (Knowledge Graph) - **μƒμœ„/루트:** [[JavaScript Tutorial]] - **κ΄€λ ¨ κ°œλ…:** [[JavaScript Booleans]], [[JavaScript Comparisons]], [[JavaScript If Else]], [[JavaScript Ternary]] - **μ°Έμ‘° λ§₯락:** Used to build compound conditions in `if`/`while`/ternary expressions and to supply safe default values. ## πŸ“š 좜처 (Sources) - [S1] W3Schools β€” JavaScript Logical Operators β€” https://www.w3schools.com/js/js_logical.asp ## πŸ“ λ³€κ²½ 이λ ₯ (Change history) - 2026-06-23: Initial draft synthesized from the W3Schools "JavaScript Logical Operators" page (Astra wiki-curation, P-Reinforce v3.1 format).