[ACCEPTED]-JavaScript equality transitivity is weird-transitivity
'' == '0' // false
The left hand side is an empty string, and 24 the right hand side is a string with one 23 character. They are false because it is 22 making a comparison between two un identical 21 strings (thanks Niall).
0 == '' // true
Hence, why this one is 20 true, because 0
is falsy and the empty string 19 is falsy.
0 == '0' // true
This one is a bit trickier. The spec 18 states that if the operands are a string 17 and a number, then coerce the string to 16 number. '0'
becomes 0
. Thanks smfoote.
false == undefined // false
The value undefined
is 15 special in JavaScript and is not equal to 14 anything else except null
. However, it is falsy.
false == null // false
Again, null
is 13 special. It is only equal to undefined
. It is also 12 falsy.
null == undefined // true
null
and undefined
are similar, but not the same. null
means 11 nothing, whilst undefined
is the value for a variable not 10 set or not existing. It would kind of make 9 sense that their values would be considered 8 equal.
If you want to be really confused, check 7 this...
'\n\r\t' == 0
A string consisting only of whitespace 6 is considered equal to 0.
Douglas Crockford 5 makes a lot of recommendations, but you 4 don't have to take them as gospel. :)
T.J. Crowder makes 3 an excellent suggestion of studying the 2 ECMAScript Language Specification to know the whole story behind these equality 1 tests.
Further Reading?
The answer to this question has to do with 12 how JavaScript handles coercion. In the 11 case of ==
, strings are coerced to be numbers. Therefore:
'' == '0'
is equivalent to 10 '' === '0'
(both are strings, so no coercion is necessary).
0 == ''
is 9 equivalent to 0 === 0
because the string ''
becomes 8 the number 0
(math.abs('') === 0
).
0 == '0'
is equivalent to 0 === 0
for the 7 same reason.
false == undefined
is equivalent to 0 === undefined
because JavaScript 6 coerces booleans to be numbers when types 5 don't match
false == null
is equivalent to 0 === null
for the same 4 reason.
null == undefined
is true because the spec says so.
Thanks 3 for asking this question. My understanding 2 of ==
is much better for having researched 1 it.
You can actually write a JavaScript function 8 that behaves exactly like ==
that should give 7 you some insight into how it behaves.
To 6 show you what I mean here is that function:
// loseEqual() behaves just like `==`
function loseEqual(x, y) {
// notice the function only uses "strict" operators
// like `===` and `!==` to do comparisons
if(typeof y === typeof x) return y === x;
if(typeof y === "function" || typeof x === "function") return false;
// treat null and undefined the same
var xIsNothing = (y === undefined) || (y === null);
var yIsNothing = (x === undefined) || (x === null);
if(xIsNothing || yIsNothing) return (xIsNothing && yIsNothing);
if(typeof x === "object") x = toPrimitive(x);
if(typeof y === "object") y = toPrimitive(y);
if(typeof y === typeof x) return y === x;
// convert x and y into numbers if they are not already use the "+" trick
if(typeof x !== "number") x = +x;
if(typeof y !== "number") y = +y;
return x === y;
}
function toPrimitive(obj) {
var value = obj.valueOf();
if(obj !== value) return value;
return obj.toString();
}
As 5 you can see ==
has a lot of complicated logic 4 for type conversion. Because of that it's 3 hard to predict what result you are going 2 to get.
Here are some examples of some results 1 you wouldn't expect:
Unexpected Truths
[1] == true // returns true
'0' == false // returns true
[] == false // returns true
[[]] == false // returns true
[0] == false // returns true
'\r\n\t' == 0 // returns true
Unexpected Conclusions
// IF an empty string '' is equal to the number zero (0)
'' == 0 // return true
// AND the string zero '0' is equal to the number zero (0)
'0' == 0 // return true
// THEN an empty string must be equal to the string zero '0'
'' == '0' // returns **FALSE**
Objects with Special Functions
// Below are examples of objects that
// implement `valueOf()` and `toString()`
var objTest = {
toString: function() {
return "test";
}
};
var obj100 = {
valueOf: function() {
return 100;
}
};
var objTest100 = {
toString: function() {
return "test";
},
valueOf: function() {
return 100;
}
};
objTest == "test" // returns true
obj100 == 100 // returns true
objTest100 == 100 // returns true
objTest100 == "test" // returns **FALSE**
More Related questions
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.