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?
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?
Share Improve this question asked Nov 15, 2016 at 17:15 Arkadiusz KałkusArkadiusz Kałkus 18.4k20 gold badges76 silver badges112 bronze badges 4 |2 Answers
Reset to default 15This 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 asType(y)
, then ... ...If
x
is null andy
is undefined, return true.- If
x
is undefined andy
isnull
, return true. - If
Type(x)
is Number andType(y)
is String, return the result of thecomparison x == ToNumber(y)
. - If
Type(x)
is String andType(y)
is Number, return the result of the comparisonToNumber(x) == y
. - If
Type(x)
is Boolean, return the result of the comparisonToNumber(x) == y
. - If
Type(y)
is Boolean, return the result of the comparisonx == ToNumber(y)
. - If
Type(x)
is either String or Number andType(y)
is Object, return the result of thecomparison x == ToPrimitive(y)
. - If
Type(x)
is Object andType(y)
is either String or Number, return the result of the comparisonToPrimitive(x) == y
. - Return false.
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
1
and2
are both truthy, would you expect1 == 2
to betrue
? – James Thorpe Commented Nov 15, 2016 at 17:18undefined != 0
and I guess that's just how it is. See there for the complete list of JS comparisons: dorey.github.io/JavaScript-Equality-Table – laurent Commented Nov 15, 2016 at 17:19