JavaScript Roadmap — Day 6: Type Conversion and Coercion
Type Conversion and Coercion
JavaScript secretly changes your data types behind your back. This is called coercion — and it is responsible for some of the most confusing bugs beginners ever face.
Look at this code carefully and predict what it will print before reading the answers:
Answers:
"53" — string + number = string concatenation
2 — string - number = number subtraction
2 — true converts to 1
1 — false converts to 0
"5" — empty string + number = string
15 — both strings convert to numbers for multiplication
If any of those surprised you — this lesson is exactly what you need. JavaScript is doing something called type coercion — silently converting your data from one type to another without telling you. Understanding this completely removes an entire category of bugs from your code forever.
1. Explicit vs Implicit — You vs JavaScript
There are two ways a type changes in JavaScript. Either you deliberately convert it — or JavaScript secretly converts it for you:
| Type | Who does it | Example | Safe? |
|---|---|---|---|
| Explicit Conversion | You — intentionally | Number("5") |
✅ Predictable |
| Implicit Coercion | JavaScript — secretly | "5" - 3 |
⚠️ Unpredictable |
2. Explicit Conversion — You Are in Control
JavaScript gives you built-in functions to convert types manually. These are predictable, safe, and always do exactly what you expect. Every professional JavaScript developer uses these daily:
3. Implicit Coercion — JavaScript's Secret Decisions 🔥
When you use operators like +, -, *, /, JavaScript decides what type to convert things to. Its decisions are not always obvious:
The + Operator — The Sneaky One
Other Operators — Number Always Wins
4. The == Operator — The Most Dangerous Coercion 🔥
Loose equality == converts types before comparing. Study these carefully — they appear in every JavaScript interview:
🔥 The rule that saves everything: Always use === instead of ==. The only exception is value == null which catches both null and undefined — a pattern senior developers use intentionally.
5. Pro Tips
NaN using isNaN() or Number.isNaN() afterward. User input like "abc" will produce NaN silently and every calculation involving it will also be NaN.parseInt("10", 10) not just parseInt("10"). Without the radix older browsers could interpret strings starting with "0" as octal (base 8). Small habit, real bug prevention.Boolean([]) is true but [] == false is also true. Because == does not use Boolean conversion — it converts both sides to numbers first. Classic interview trap question.6. Quick Reference — Conversion Rules
| Value | → Number | → String | → Boolean |
|---|---|---|---|
0 | 0 | "0" | ❌ false |
1 | 1 | "1" | ✅ true |
"" | 0 | "" | ❌ false |
"0" | 0 | "0" | ✅ true |
"hello" | NaN | "hello" | ✅ true |
true | 1 | "true" | ✅ true |
false | 0 | "false" | ❌ false |
null | 0 | "null" | ❌ false |
undefined | NaN | "undefined" | ❌ false |
[] | 0 | "" | ✅ true |
7. Try It Yourself
Run the code and study every output. Then try changing the operators and values to test your understanding.
8. Practice Tasks
Task 1 — Easy: Predict Before Running
Before running — write your predicted output next to each line. Then run and check: "10" + 5, "10" - 5, true + true + true, null + undefined, "5" * false.
Task 2 — Medium: Safe Input Converter
Write a function safeToNumber(value) that converts any input to a number. If the result is NaN return 0 as fallback. Test with: "42", "hello", true, null, undefined, "".
Task 3 — Hard: The Coercion Detective
These all return true with ==. Explain exactly why for each: [] == false, "" == 0, [1] == 1, null == undefined. Then rewrite with === and explain why they return false.