--- id: javascript-comparisons title: "JavaScript Comparisons" category: "Frontend" status: "draft" verification_status: "conceptual" canonical_id: "" aliases: ["JS comparison operators", "comparison operators", "equality operators", "strict equality", "string comparison", "type coercion comparison"] duplicate_of: "" source_trust_level: "B" confidence_score: 0.88 created_at: 2026-06-23 updated_at: 2026-06-23 review_reason: "" merge_history: [] tags: ["javascript", "js", "web", "frontend", "w3schools", "comparisons", "operators"] raw_sources: ["https://www.w3schools.com/js/js_comparisons.asp"] applied_in: [] github_commit: "" --- # [[JavaScript Comparisons]] ## 🎯 ν•œ 쀄 톡찰 (One-line insight) Comparison operators compare two values and always return `true` or `false`; they work on strings (compared alphabetically) and coerce a string to a number when comparing a string with a number. [S1] ## 🧠 핡심 κ°œλ… (Core concepts) - **Comparison operators compare two values** β€” they always return `true` or `false`. [S1] - **Loose vs strict equality** β€” `==` checks equal value; `===` checks equal value *and* equal type (and `!=` vs `!==` likewise). [S1] - **String comparison is alphabetical** β€” all comparison operators can be used on strings, and strings are compared alphabetically. [S1] - **Type coercion on mixed compare** β€” when comparing a string with a number, JavaScript converts the string to a number; an empty string converts to `0`, and a non-numeric string converts to `NaN`, which is always `false`. [S1] - **String-vs-string is lexical** β€” when comparing two strings, `"2"` is greater than `"12"` because, alphabetically, `1` is less than `2`. [S1] ## 🧩 μΆ”μΆœλœ νŒ¨ν„΄ (Extracted patterns) - **Prefer `===` for safety** β€” use strict equality to avoid surprising coercions, since `x == "5"` is `true` but `x === "5"` is `false` when `x` is the number 5. [S1] - **Guard numeric input** β€” `Number(age)` then `isNaN(...)` before comparing handles non-numeric input. [S1] - **Beware lexical string compare** β€” comparing numeric-looking strings compares them character by character, not numerically. [S1] ## πŸ“– μ„ΈλΆ€ λ‚΄μš© (Details) **Comparison Operators** [S1] Comparison operators are used to compare two values. Comparison operators always return `true` or `false`. Assuming `x = 5`: | Operator | Description | Comparing | Returns | |----------|-------------|-----------|---------| | == | equal to | x == 8 | false | | == | equal to | x == 5 | true | | == | equal to | x == "5" | true | | === | equal value and equal type | x === 5 | true | | === | equal value and equal type | x === "5" | false | | != | not equal | x != 8 | true | | !== | not equal value or not equal type | x !== 5 | false | | !== | not equal value or not equal type | x !== "5" | true | | !== | not equal value or not equal type | x !== 8 | true | | > | greater than | x > 8 | false | | < | less than | x < 8 | true | | >= | greater than or equal to | x >= 8 | false | | <= | less than or equal to | x <= 8 | true | **JavaScript String Comparison** [S1] All the comparison operators above can also be used on strings. Strings are compared alphabetically: ```javascript let text1 = "A"; let text2 = "B"; let result = text1 < text2; ``` Note that, when comparing two strings, the comparison is lexical, not numeric: ```javascript let text1 = "20"; let text2 = "5"; let result = text1 < text2; ``` **Comparing Different Types** [S1] When comparing a string with a number, JavaScript will convert the string to a number when doing the comparison. An empty string converts to `0`. A non-numeric string converts to `NaN`, which is always `false`. When comparing two strings, `"2"` will be greater than `"12"`, because (alphabetically) `1` is less than `2`. | Case | Value | |------|-------| | 2 < 12 | true | | 2 < "12" | true | | 2 < "John" | false | | 2 > "John" | false | | 2 == "John" | false | | "2" < "12" | false | | "2" > "12" | true | | "2" == "12" | false | **Conditional (Ternary) Operator** [S1] The page demonstrates guarding numeric input and then using a ternary expression for the decision: ```javascript age = Number(age); if (isNaN(age)) { voteable = "Input is not a number"; } else { voteable = (age < 18) ? "Too young" : "Old enough"; } ``` ## πŸ› οΈ 적용 사둀 (Applied in summary) The page's own snippets are the canonical applied examples β€” comparing strings alphabetically, comparing numeric strings lexically, and guarding input with `Number(...)` + `isNaN(...)` before a ternary decision. No external project/commit applications found in the source. ## πŸ’» μ½”λ“œ νŒ¨ν„΄ (Code patterns) Alphabetical string comparison: ```javascript let text1 = "A"; let text2 = "B"; let result = text1 < text2; ``` Validate then decide: ```javascript age = Number(age); if (isNaN(age)) { voteable = "Input is not a number"; } else { voteable = (age < 18) ? "Too young" : "Old enough"; } ``` ## βš–οΈ 비ꡐ 및 선택 κΈ°μ€€ (Comparison & decision criteria) - **`==` (loose) vs `===` (strict)** β€” `==` compares value after type coercion, so `5 == "5"` is `true`; `===` requires equal value *and* type, so `5 === "5"` is `false`. Prefer `===`/`!==` when you want to avoid implicit coercion surprises. [S1] - **Number vs string comparison** β€” with `<`/`>`, a string operand is coerced to a number (empty string β†’ `0`, non-numeric β†’ `NaN` β†’ `false`); but when *both* operands are strings, the comparison is alphabetical (lexical), so `"2" > "12"` is `true`. [S1] ## βš–οΈ λͺ¨μˆœ 및 μ—…λ°μ΄νŠΈ (Contradictions & updates) No contradictions found in the source. The apparent paradox that `2 < "12"` is `true` while `"2" < "12"` is `false` is explained by coercion-vs-lexical comparison, not an inconsistency. ## βœ… 검증 μƒνƒœ 및 신뒰도 - **μƒνƒœ:** draft - **검증 단계:** conceptual (μ‹€μ œ 적용 사둀 발견 μ‹œ applied/validated둜 승격 κ°€λŠ₯) - **좜처 신뒰도:** B (W3Schools β€” widely used educational reference, not a primary standards body) - **μ‹ λ’° 점수:** 0.88 - **쀑볡 검사 κ²°κ³Ό:** μ‹ κ·œ 생성 (New discovery) ## πŸ”— 지식 κ·Έλž˜ν”„ (Knowledge Graph) - **μƒμœ„/루트:** [[JavaScript Tutorial]] - **κ΄€λ ¨ κ°œλ…:** [[JavaScript Operators]], [[JavaScript Conditional Operators]], [[JavaScript If]], [[JavaScript Types]] - **μ°Έμ‘° λ§₯락:** Referenced whenever evaluating a condition for branching, loops, or validation. ## πŸ“š 좜처 (Sources) - [S1] W3Schools β€” JavaScript Comparisons β€” https://www.w3schools.com/js/js_comparisons.asp ## πŸ“ λ³€κ²½ 이λ ₯ (Change history) - 2026-06-23: Initial draft synthesized from the W3Schools "JavaScript Comparisons" page (Astra wiki-curation, P-Reinforce v3.1 format).