--- id: javascript-silent-errors title: "JavaScript Silent Errors" category: "Frontend" status: "draft" verification_status: "conceptual" canonical_id: "" aliases: ["silent errors", "type coercion", "loose equality", "NaN", "weakly typed"] duplicate_of: "" source_trust_level: "B" confidence_score: 0.86 created_at: 2026-06-23 updated_at: 2026-06-23 review_reason: "" merge_history: [] tags: ["javascript", "js", "web", "frontend", "w3schools", "errors", "coercion"] raw_sources: ["https://www.w3schools.com/js/js_errors_silent.asp"] applied_in: [] github_commit: "" --- # [[JavaScript Silent Errors]] ## 🎯 ν•œ 쀄 톡찰 (One-line insight) Silent errors don't stop execution β€” JavaScript keeps running while producing wrong results from things like `1/0`, accidental assignment in conditions, failed `parseInt`, and type coercion. [S1] ## 🧠 핡심 κ°œλ… (Core concepts) - **Silent errors won't stop the program** β€” unlike thrown exceptions, silent errors let execution continue while quietly yielding wrong values. [S1] - **Historical context** β€” early JavaScript lacked exception handling, contributing to silent-error behavior. [S1] - **Failed numeric operations produce NaN** β€” many numeric operations that fail produce `NaN` rather than throwing an exception. [S1] - **Type coercion** β€” JavaScript is weakly typed and automatically converts between data types. [S1] - **String vs numeric coercion** β€” `+` with any string operand makes everything a string; other arithmetic operators force values to numbers. [S1] - **Loose equality (`==`)** coerces operands, so `5 == "5"` is `true`. [S1] ## 🧩 μΆ”μΆœλœ νŒ¨ν„΄ (Extracted patterns) - **`=` vs `==` trap** β€” using assignment (`=`) inside an `if` condition silently sets the variable and runs the block instead of comparing. [S1] - **Operator decides the type** β€” `'5' + '2'` concatenates to `"52"`; `'5' - '2'` subtracts to `3`. The operator, not the operands, dictates coercion. [S1] - **Prefer strict equality and explicit conversion** β€” use `===`, convert types explicitly, and watch for `NaN` results to avoid silent bugs. [S1] ## πŸ“– μ„ΈλΆ€ λ‚΄μš© (Details) **Silent Errors** Silent errors won't stop program execution. Historically, early JavaScript lacked exception handling, which contributed to this behavior. [S1] Division by zero does not throw β€” it yields `Infinity` silently: [S1] ```javascript let x = 1 / 0; ``` Accidental assignment inside a condition β€” the condition uses assignment (`isActive = true`) rather than comparison, setting the variable to true and executing the block: [S1] ```javascript let result = "Not Active."; let isActive = false; // ❌ Assignment, not comparison if (isActive = true) { let result = "Active!"; } ``` Failed parsing produces `NaN`, not an exception. Many numeric operations that fail produce `NaN` (not an exception): [S1] ```javascript const result = parseInt("abc"); ``` Accessing a missing object property yields `undefined` silently: [S1] ```javascript const user = {}; let result = user.name; ``` **Type Coercion** JavaScript is weakly typed and automatically converts between data types. [S1] ```javascript let result1 = ('5' + '2'); // = 52 let result2 = ('5' - '2'); // = 3 ``` **String Coercion (+)** If any part of a `+` operation is a string, JavaScript converts everything to strings: [S1] ```javascript let x = "5" + 2 // x = "52" ``` **Numeric Coercion** Other operators force values to numbers: [S1] ```javascript let x = "5" - 2 // x = 3 ``` **Loose Equality (==)** Loose equality coerces operands before comparing: [S1] ```javascript let x = (5 == "5") // x = true ``` **Practices to Avoid Bugs** Three recommendations: use strict equality (`===`), be explicit with conversions, and watch for `NaN` results. [S1] ## πŸ› οΈ 적용 사둀 (Applied in summary) The page's snippets demonstrate the silent-error patterns directly β€” `1/0`, the `=` inside `if`, `parseInt("abc")`, missing properties, and coercion under `+`/`-`/`==`. No external project/commit applications found in the source. ## πŸ’» μ½”λ“œ νŒ¨ν„΄ (Code patterns) Avoid the assignment-in-condition trap and prefer strict equality (language: JavaScript): ```javascript // ❌ assignment, always truthy if (isActive = true) { /* ... */ } // βœ… strict comparison if (isActive === true) { /* ... */ } ``` Coercion cheat sheet: ```javascript '5' + '2' // "52" (string concatenation) '5' - '2' // 3 (numeric subtraction) "5" + 2 // "52" "5" - 2 // 3 5 == "5" // true (loose equality coerces) parseInt("abc") // NaN (no exception thrown) ``` ## βš–οΈ λͺ¨μˆœ 및 μ—…λ°μ΄νŠΈ (Contradictions & updates) No contradictions found in the source. ## βœ… 검증 μƒνƒœ 및 신뒰도 - **μƒνƒœ:** draft - **검증 단계:** conceptual (μ‹€μ œ 적용 사둀 발견 μ‹œ applied/validated둜 승격 κ°€λŠ₯) - **좜처 신뒰도:** B (W3Schools β€” widely used educational reference, not a primary standards body) - **μ‹ λ’° 점수:** 0.86 - **쀑볡 검사 κ²°κ³Ό:** μ‹ κ·œ 생성 (New discovery) ## πŸ”— 지식 κ·Έλž˜ν”„ (Knowledge Graph) - **μƒμœ„/루트:** [[JavaScript Tutorial]] - **κ΄€λ ¨ κ°œλ…:** [[JavaScript Errors Intro]], [[JavaScript Error Statements]], [[JavaScript Comparisons]], [[JavaScript Type Conversion]] - **μ°Έμ‘° λ§₯락:** Complements the thrown-error pages by covering failures that do not raise exceptions and must be caught by careful coding. ## πŸ“š 좜처 (Sources) - [S1] W3Schools β€” JavaScript Silent Errors β€” https://www.w3schools.com/js/js_errors_silent.asp ## πŸ“ λ³€κ²½ 이λ ₯ (Change history) - 2026-06-23: Initial draft synthesized from the W3Schools "JavaScript Silent Errors" page (Astra wiki-curation, P-Reinforce v3.1 format).