Type Coercion: Safe Patterns to Avoid Surprises
Understand implicit vs explicit coercion; prefer clear casts with Number/String/Boolean; handle NaN safely.
Intro
Goal: See when JavaScript converts types, what results it produces, and how to use explicit casts to stay predictable.
- Implicit vs explicit
- Number/String/Boolean
- NaN safety

Implicit coercion demo
+ with a string triggers concatenation; - and * try numeric conversion.
// Implicit coercion can surprise beginners
console.log("5" + 1);
// "51" (string concatenation)
console.log(5 + "1");
// "51" (number -> string)
console.log("5" - 1);
// 4 (string -> number)
console.log("5" * 2);
// 10 (string -> number)

All lessons in this course
- Primitives vs Objects & Core Types
- typeof, Truthy/Falsy, and == vs ===
- Type Coercion: Safe Patterns to Avoid Surprises