The code is correct. The result is wrong. JavaScript looks at you… and smiles.
When writing JavaScript, you sometimes feel this:
“I’m learning this language…
but it feels like the language is trying to analyze me.”
The reason is clear:
Type coercion, reference comparisons, and JavaScript’s
‘I know better than you’ attitude.
Let’s expose them one by one.
1️⃣ "5" + 1 → "51"
“I wanted addition, it started texting.”
"5" + 1
// "51"
What happened?
The + operator has two personalities:
- Sees numbers → does math
- Sees a string → does string concatenation
JavaScript thought:
“One is a string… fine, everyone becomes a string.”
String(1) → "1"
"5" + "1" → "51"
📌 Tip:
Number("5") + 1 // 6
Lesson:+ is unreliable.
If you want math, you decide the type.
2️⃣ "5" - 1 → 4
“It was a string a second ago… now it’s doing math?”
"5" - 1
// 4
Why the different behavior?
- The
-operator does not understand strings - It forces them into numbers
Number("5") - 1 // 4
📌 JavaScript personality summary:
+→ indecisive- * /→ mathematician
3️⃣ [] == false → true
“At this point JavaScript is testing me.”
[] == false
// true
Step-by-step disaster:
[] → ""
"" → 0
false → 0
0 == 0 → true
📌 Reality:== doesn’t compare values —
it first tries to make everyone look the same.
❌ Don’t do this:
if (value == false)
✅ Do this:
if (value === false)
4️⃣ [] == ![] → true
“This is personal now.”
[] == ![]
// true
What happened:
![] → false
[] → ""
"" → 0
false → 0
Result:
0 == 0 // true
📌 Lesson:! + == + array = chaos
5️⃣ [1,2] == "1,2" → true
“Is it an array or a string? Nobody knows.”
[1,2] == "1,2"
// true
Why?
[1,2].toString() // "1,2"
Then:
"1,2" == "1,2"
📌 Golden rule:
Never compare arrays with ==.
Your heart will break.
6️⃣ NaN === NaN → false
“Not even equal to itself.”
NaN === NaN
// false
Because:
NaNmeans Not a Number- It represents an undefined result
- Undefined things cannot be compared
📌 Correct check:
Number.isNaN(value)
❌ Don’t do:
value === NaN
7️⃣ typeof NaN → "number"
“This is where acceptance begins.”
typeof NaN
// "number"
JavaScript says:
“Yes, it’s not a number…
but it belongs to the number category.”
📌 Fact:
This is one of JavaScript’s oldest bugs
and is now officially considered a “feature”.
8️⃣ null == undefined → true
“Separated, but treated the same.”
null == undefined
// true
But:
null === undefined
// false
Explanation:
==→ “They’re both basically nothing”===→ “But they are NOT the same thing”
📌 Practical check:
value == null
// catches both null and undefined
(Useful if used consciously.)
9️⃣ 0.1 + 0.2 !== 0.3
“Is even basic math too much?”
0.1 + 0.2
// 0.30000000000000004
Reason:
- Floating-point precision
- Binary number system
📌 Solutions:
Math.round((0.1 + 0.2) * 100) / 100
or:
+(0.1 + 0.2).toFixed(2)
🔟 [] === [] → false
“The same… but not.”
[] === []
// false
Why?
- Arrays and objects are compared by reference, not value
let a = [];
let b = a;
a === b // true
📌 Lesson:
For objects,
“same content” ≠ “same thing”
🧠 Final Truth (Save This)
JavaScript is not trolling you.
Its rules are just hidden.
Survival guide:
✅ Use ===
❌ Stay away from ==
🧠 Do type conversion yourself
🤡 When things get weird, don’t say “I’m stupid” —
blame JavaScript first. 😄
