JavaScript Runtime Semantics
Coercion & Equality
JavaScript can convert values explicitly or implicitly. Strict equality avoids most implicit conversion, while loose equality follows a set of coercion rules that are precise but rarely obvious at a glance.
- Coercion is conversion between runtime value types
===compares without type coercion==is not random, but it is non-local- NaN and -0 require special sameness awareness
- TypeScript does not remove coercion from runtime
| Tool | Does coercion? | Use for |
|---|---|---|
| == | Yes | Rare intentional nullish checks like x == null |
| === | No type coercion | Default equality |
| Object.is | SameValue semantics | NaN and -0-sensitive checks |
| Number.isNaN | No coercion | Check actual NaN |
| Boolean(value) | Explicit truthiness | Clear condition conversion |
0 == false // true
0 === false // false
null == undefined // true
null === undefined // false
NaN === NaN // false
Object.is(NaN, NaN) // true
Object.is(0, -0) // false