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 🔥
This is where JavaScript gets wild. When you use operators like +, -, *, /, or comparisons like ==, JavaScript has to decide what type to convert things to. Its decisions are not always obvious. Let us go through every rule:
The + Operator — The Sneaky One
The + operator has a split personality. It does addition for numbers but concatenation for strings. And when it sees a mix — it always chooses string concatenation. This surprises everyone at first:
Other Operators — Number Always Wins
For -, *, /, and % there is no string version. So JavaScript always tries to convert everything to a number. This is why "5" - 3 gives 2 but "5" + 3 gives "53":
4. The == Operator — The Most Dangerous Coercion 🔥
Loose equality == converts types before comparing. This leads to results that look completely insane until you understand the rules. Study these carefully — they appear in every JavaScript interview:
🔥 The rule that saves everything: Always use === instead of == in your code. The only exception is checking for null and undefined together with value == null — which is actually a clean, readable pattern that senior developers use intentionally.
NaN using isNaN() or Number.isNaN() afterward. User input like "abc" or an empty field will produce NaN silently and then every calculation involving it will also be NaN. Always validate first.parseInt. Write parseInt("10", 10) not just parseInt("10"). Without the radix, older browsers could interpret strings starting with "0" as octal (base 8). It is a small habit that prevents a real bug.Boolean([]) is true — empty array is truthy. But [] == false is also true. How can something be truthy AND equal to false? Because == does not use Boolean conversion — it converts both sides to numbers first. This is one of the most confusing JavaScript quirks and a common interview trick question.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 |
Try It Yourself
Run the code and study every output. Then try changing the operators and values to test your understanding.
Practice Tasks
Task 1 — Easy: Predict Before Running
Before running the code — write your predicted output in comments next to each line. Then run it and see how many you got right. Do this for: "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 it should return 0 as a fallback. Test it with: "42", "hello", true, null, undefined, "".
Task 3 — Hard: The Coercion Detective
These expressions all return true with ==. For each one — explain in a comment exactly why JavaScript considers them equal and what conversions happened: [] == false, "" == 0, [1] == 1, null == undefined. Then rewrite each using === and explain why they now return false.