Type Coercion
JavaScript automatically converts types when needed
1. Implicit Coercion (Automatic)
Example:
console.log("5" + 2);
// "52"
👉 Number → converted to string
console.log("5" - 2);
// 3
👉 String → converted to number
console.log(true + 1); // 2
👉 true → 1
2. Explicit Coercion (Manual)
You convert types yourself:
Number("10"); // 10
String(100); // "100"
Boolean(0); // false
Mini Practice
let a = "10"; let b = 5; console.log(a + b); console.log(a - b); console.log(a * b);
👉 Try to predict output before running
Quick Rule (VERY IMPORTANT)
+→ string concatenation if one is string-,*,/→ always convert to number