JavaScript performs type conversion in two ways: implicit coercion (automatic, done by the engine) and explicit conversion (manual, done by the developer). Understanding the difference is essential to avoid bugs. Implicit Coercion Implicit coercion happens when you use operators on values of different types. The + operator with a string triggers string concatenation: "5" + 3 becomes "53". The -, , / operators convert strings to numbers: "5" - 3 becomes 2. Comparison with == converts types before comparing — known as "the equality trap." With booleans, true becomes 1 and false becomes 0. Explicit Conversion Explicit conversion (preferred): Number() converts to a number — Number("42") → 42, Number("") → 0, Number("abc") → NaN. parseInt() and parseFloat() parse strings for numbers. String()…
Key Takeaways
Implicit coercion is automatic and can cause subtle bugs.
Explicit conversion (Number(), String(), Boolean()) is predictable.
Falsy values: 0, "", null, undefined, NaN, false.
Empty arrays [] and objects {} are truthy — common surprise.
Use Number.isNaN() not global isNaN() for NaN checks.