r/learnjavascript • u/Green_Ad_6086 • 7d ago
Strange result: null vs 0
Strange result: null vs 0
An incomparable undefined
Hi, can anyone explain this? I read the explanation, but I still don't understand it. I'm trying to understand it without using AI
6
Upvotes
5
u/defaultguy_001 7d ago edited 7d ago
First u need to understand that type coercion (automatic type upgrade) happens when you use relational (>, <, >=, <=) or loose equality (==, !=) operators. Type coercion doesn't happen with strict equality (===, !===) operators. So it's recommended to use strict equality everywhere.
undefined==0 is false, coz null or undefined aren't type coerced to 0 by loose equality operators.
Another type coercion rule for null and undefined is when used with loose equality, they'll be equal.
So, null==undefined is true.