---
id: javascript-type-coercion
title: "JavaScript Type Coercion"
category: "Frontend"
status: "draft"
verification_status: "conceptual"
canonical_id: ""
aliases: ["type coercion", "JS coercion", "implicit conversion", "loose equality", "weakly typed", "truthy falsy"]
duplicate_of: ""
source_trust_level: "B"
confidence_score: 0.87
created_at: 2026-06-23
updated_at: 2026-06-23
review_reason: ""
merge_history: []
tags: ["javascript", "js", "web", "frontend", "w3schools", "type-coercion", "equality"]
raw_sources: ["https://www.w3schools.com/js/js_type_coercion.asp"]
applied_in: []
github_commit: ""
---

# [[JavaScript Type Coercion]]

## 🎯 한 줄 통찰 (One-line insight)
Type coercion is JavaScript's automatic conversion of values from one data type to another, applied implicitly per-operator because the language is weakly typed — which can quietly hide bugs. [S1]

## 🧠 핵심 개념 (Core concepts)
- **Automatic conversion** — Type coercion is the automatic conversion of values from one data type to another. [S1]
- **Triggered by mixed-type operations** — It happens when you operate on different data types and the engine tries to make them "fit" together. [S1]
- **Per-operator behavior** — JavaScript coerces types differently per operator, so coercion can hide bugs. [S1]
- **Implicit vs explicit** — Coercion is implicit (handled by the engine), while type conversion is explicit (you manually use functions like `Number()` or `String()`). [S1]
- **Weakly typed** — Because JavaScript is weakly typed, it doesn't throw errors when types don't match; it just tries to coerce them. [S1]
- **Truthy / falsy** — Falsy values are `0`, `""`, `null`, `undefined`, `NaN`, and `false`; everything else is truthy (including empty objects `{}` and arrays `[]`). [S1]

## 🧩 추출된 패턴 (Extracted patterns)
- **`+` concatenates, others compute** — If any part of a `+` operation is a string, everything becomes a string and is concatenated; other arithmetic operators (`-`, `*`, `/`, `%`) and unary `+` force values into numbers. [S1]
- **Boolean context coercion** — Values are coerced to booleans in logical contexts like `if` statements or with the double-NOT operator (`!!`). [S1]
- **Loose `==` finds a common type** — Loose equality coerces operands to a common type before comparing. [S1]
- **Prefer `===` and explicit conversion** — Use strict equality and explicit `Number()`/`String()` to avoid surprising results. [S1]

## 📖 세부 내용 (Details)
**Type Coercion**
Type coercion is the automatic conversion of values from one data type to another. It happens when you perform an operation on different data types, and the JavaScript engine tries to make them "fit" together. JavaScript coerces types differently per operator, so type coercion can hide bugs; the program continues according to the coercion rules, but these may differ from what the programmer wanted to achieve. [S1]
```javascript
let result1 = ('5' + '2'); // = 52
let result2 = ('5' - '2'); // = 3
```
Coercion is implicit (handled automatically by the engine), while type conversion is explicit (you manually use functions like `Number()` or `String()`). Because JavaScript is weakly typed, it doesn't throw errors when types don't match; it just tries to coerce them. [S1]

**String Coercion (+)**
If any part of a `+` operation is a string, JavaScript converts everything to strings and concatenates them. [S1]
```javascript
let x = "5" + 2 // x = "52"
```

**Numeric Coercion**
Other arithmetic operators (`-`, `*`, `/`, `%`) and the unary plus (`+x`) force values into numbers. [S1]
```javascript
let x = "5" - 2 // x = 3
```

**Boolean Coercion**
Values are coerced to booleans in logical contexts like `if` statements or using the double-NOT operator (`!!`). Falsy values: `0`, `""`, `null`, `undefined`, `NaN`, and `false`. Truthy values: Everything else (including empty objects `{}` and arrays `[]`). [S1]

**Loose Equality (==)**
This operator performs coercion to find a "common type" before comparing. [S1]
```javascript
let x = (5 == "5") // x = true
```

**Best Practices to Avoid Bugs**
- **Use `===` (Strict Equality):** This checks both value and type without coercion, preventing weird results. [S1]
- **Be Explicit:** Instead of relying on automatic behavior, use recommended conversion methods like `Number()` or `String()` to make your intent clear. [S1]
- **Watch for NaN:** If a string cannot be converted to a valid number (like `"abc" - 1`), the result is `NaN` (Not-a-Number). [S1]

## 🛠️ 적용 사례 (Applied in summary)
The page's own snippets are the canonical applied examples — `'5' + '2'` vs `'5' - '2'`, string concatenation `"5" + 2`, numeric coercion `"5" - 2`, and loose equality `5 == "5"`. No external project/commit applications found in the source.

## 💻 코드 패턴 (Code patterns)
Same operands, opposite outcomes by operator (language: JavaScript):
```javascript
let result1 = ('5' + '2'); // = 52  (string concatenation)
let result2 = ('5' - '2'); // = 3   (numeric subtraction)
```
Loose equality coerces before comparing:
```javascript
let x = (5 == "5"); // x = true
```

## ⚖️ 비교 및 선택 기준 (Comparison & decision criteria)
- **`==` vs `===`** — Loose equality (`==`) coerces operands to a common type, so `5 == "5"` is `true`; strict equality (`===`) compares value and type without coercion. The page recommends `===` to prevent surprising results. [S1]
- **Implicit coercion vs explicit conversion** — Coercion is automatic and engine-driven; explicit conversion via `Number()`/`String()` makes intent clear and is the recommended practice to avoid hidden bugs. [S1]

## ⚖️ 모순 및 업데이트 (Contradictions & updates)
No contradictions found in the source.

## ✅ 검증 상태 및 신뢰도
- **상태:** draft
- **검증 단계:** conceptual (실제 적용 사례 발견 시 applied/validated로 승격 가능)
- **출처 신뢰도:** B (W3Schools — widely used educational reference, not a primary standards body)
- **신뢰 점수:** 0.87
- **중복 검사 결과:** 신규 생성 (New discovery)

## 🔗 지식 그래프 (Knowledge Graph)
- **상위/루트:** [[JavaScript Tutorial]]
- **관련 개념:** [[JavaScript Type Conversion]], [[JavaScript NaN]], [[JavaScript undefined]], [[JavaScript Introduction]]
- **참조 맥락:** Referenced whenever reasoning about `==` vs `===`, truthy/falsy checks, or unexpected results from mixed-type operations.

## 📚 출처 (Sources)
- [S1] W3Schools — JavaScript Type Coercion — https://www.w3schools.com/js/js_type_coercion.asp

## 📝 변경 이력 (Change history)
- 2026-06-23: Initial draft synthesized from the W3Schools "JavaScript Type Coercion" page (Astra wiki-curation, P-Reinforce v3.1 format).
