--- id: javascript-ternary title: "JavaScript Ternary" category: "Frontend" status: "draft" verification_status: "conceptual" canonical_id: "" aliases: ["ternary operator", "conditional operator", "JS ternary", "?: operator", "shorthand if else"] duplicate_of: "" source_trust_level: "B" confidence_score: 0.9 created_at: 2026-06-23 updated_at: 2026-06-23 review_reason: "" merge_history: [] tags: ["javascript", "js", "web", "frontend", "w3schools", "ternary", "operators", "conditional"] raw_sources: ["https://www.w3schools.com/js/js_if_ternary.asp"] applied_in: [] github_commit: "" --- # [[JavaScript Ternary]] ## 🎯 ν•œ 쀄 톡찰 (One-line insight) The conditional (ternary) operator is a shorthand for an `if...else` statement β€” it is the only JavaScript operator that takes three operands. [S1] ## 🧠 핡심 κ°œλ… (Core concepts) - **Shorthand for `if...else`** β€” the conditional operator is a shorthand for writing conditional `if...else` statements. [S1] - **Three operands** β€” it is called a ternary operator because it takes three operands, and it is the only JavaScript operator that takes three operands. [S1] - **Returns one of two values** β€” it evaluates a condition and returns `expression1` if the condition is `true`, otherwise `expression2`. [S1] - **Standardized early** β€” `() ? x : y` is an ES1 feature (JavaScript 1997) and is fully supported in all browsers. [S1] ## 🧩 μΆ”μΆœλœ νŒ¨ν„΄ (Extracted patterns) - **Inline conditional assignment** β€” assign one of two values to a variable in a single expression based on a test, e.g. `let text = (age < 18) ? "Minor" : "Adult";`. [S1] - **Flag-driven value selection** β€” use a boolean flag as the condition to pick between two literal values (e.g. a discount rate). [S1] ## πŸ“– μ„ΈλΆ€ λ‚΄μš© (Details) **The conditional (ternary) operator** [S1] The conditional operator is a shorthand for writing conditional `if...else` statements. It is called a ternary operator because it takes three operands. **Syntax** [S1] ``` (condition) ? expression1 : expression2 ``` **Assigning a value based on a condition** [S1] ```javascript let text = (age < 18) ? "Minor" : "Adult"; ``` **Flag-driven selection β€” true branch** [S1] ```javascript let isMember = true; let discount = isMember ? 0.2 : 0; ``` **Flag-driven selection β€” false branch** [S1] ```javascript let isMember = false; let discount = isMember ? 0.2 : 0; ``` **Parameters** [S1] | Parameter | Description | |-----------|-------------| | condition | Required. The condition to be tested. An expression that evaluates to `true` or `false`. | | ? | Required. The operator separating the condition from the expressions. | | expression1 | Required. The value to return if the condition is `true`. | | : | Required. The operator separating the expressions. | | expression2 | Required. The value to return if the condition is `false`. | **Note** [S1] The conditional (ternary) operator is the only JavaScript operator that takes three operands. **Browser support** [S1] `() ? x : y` is an ES1 feature (JavaScript 1997). It is fully supported in all browsers. ## πŸ› οΈ 적용 사둀 (Applied in summary) The page's own snippets are the canonical applied examples β€” selecting between `"Minor"`/`"Adult"` based on `age`, and choosing a `discount` based on an `isMember` flag. No external project/commit applications found in the source. ## πŸ’» μ½”λ“œ νŒ¨ν„΄ (Code patterns) Inline conditional assignment: ```javascript let text = (age < 18) ? "Minor" : "Adult"; ``` Flag-driven value selection: ```javascript let isMember = true; let discount = isMember ? 0.2 : 0; ``` ## βš–οΈ λͺ¨μˆœ 및 μ—…λ°μ΄νŠΈ (Contradictions & updates) No contradictions found in the source. ## βœ… 검증 μƒνƒœ 및 신뒰도 - **μƒνƒœ:** draft - **검증 단계:** conceptual (μ‹€μ œ 적용 사둀 발견 μ‹œ applied/validated둜 승격 κ°€λŠ₯) - **좜처 신뒰도:** B (W3Schools β€” widely used educational reference, not a primary standards body) - **μ‹ λ’° 점수:** 0.90 - **쀑볡 검사 κ²°κ³Ό:** μ‹ κ·œ 생성 (New discovery) ## πŸ”— 지식 κ·Έλž˜ν”„ (Knowledge Graph) - **μƒμœ„/루트:** [[JavaScript Tutorial]] - **κ΄€λ ¨ κ°œλ…:** [[JavaScript If Else]], [[JavaScript Switch]], [[JavaScript Comparisons]], [[JavaScript Booleans]] - **μ°Έμ‘° λ§₯락:** Used wherever a compact `if...else` value selection is needed inline within an expression. ## πŸ“š 좜처 (Sources) - [S1] W3Schools β€” JavaScript Ternary β€” https://www.w3schools.com/js/js_if_ternary.asp ## πŸ“ λ³€κ²½ 이λ ₯ (Change history) - 2026-06-23: Initial draft synthesized from the W3Schools "JavaScript Ternary" page (Astra wiki-curation, P-Reinforce v3.1 format).