I'm discovering odds of JavaScript comparisons. I wanted to find an example of how tricky comparisons may be, and how can cause bugs in some situations.
I thought about example where some input variable remains undefined, and is compared to zero. Because undefined is false when converted to Boolean, and zero is false when converted to Boolean I decided to test following code:
var x;
//Here x should be initialized but due to some circumstances is not
if(x == 0){
//This should run
}
Surprisingly...
Boolean(undefined) //false
Boolean(0) //false
x //undefined
x == 0 //false
Why it's like that?
It's exactly the same.
The reason behind this behavior is that JavaScript treats non-empty string as true. First, “0” is converted into its boolean value, by automatic type conversion which is true.
Both undefined and null are falsy by default. So == returns true. But when we use the strict equality operator (===) which checks both type and value, since undefined and null are of different types (from the typeof Operator section), the strict equality operator returns false.
The Boolean value of undefined is false. The value of Not only undefined but also null, false, NaN, empty string is also false.
This behaviour is in the specification for The Abstract Equality Comparison Algorithm
From the specification
The comparison x == y
, where x
and y
are values, produces true or false. Such a comparison is performed as follows:
If Type(x)
is the same as Type(y)
, then ...
...
If x
is null and y
is undefined, return true.
x
is undefined and y
is null
, return true.Type(x)
is Number and Type(y)
is String, return the result of the comparison x == ToNumber(y)
.Type(x)
is String and Type(y)
is Number, return the result of the comparison ToNumber(x) == y
.Type(x)
is Boolean, return the result of the comparison ToNumber(x) == y
.Type(y)
is Boolean, return the result of the comparison x == ToNumber(y)
.Type(x)
is either String or Number and Type(y)
is Object, return the result of the comparison x == ToPrimitive(y)
.Type(x)
is Object and Type(y)
is either String or Number, return the result of the comparison ToPrimitive(x) == y
.As undefined
and a number (0
) is not of the same type, it's only in the third point where it mentions what to do if the left hand side is undefined
.
Then, if the right hand side is null
, it returns true, any other value, and it goes straight to 10.
, which says "return false
".
Boolean(undefined) //false
Boolean(0) //false
Actually, The Boolean
function returns false
for all non-real values such as 0, null, undefined, ""(empty string), etc.
It does not mean that the undefined == 0
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With