This might happen because the two variables are of different types.
In case one of the variables holds a Date
instance and the other a String
, comparing both of them to a string literal will return true, while comparing their valueOf()
results will return false, since valueOf()
of a Date
returns number of milliseconds since epoch, not the human-readable representation of a date (as opposed to toString()
).
var a = new Date()
a.toString() //"Wed Feb 27 2013 01:41:51 GMT+0300 (MSK)"
a.valueOf() //1361918511306
var b = "Wed Feb 27 2013 01:41:51 GMT+0300 (MSK)"
b.toString() //"Wed Feb 27 2013 01:41:51 GMT+0300 (MSK)"
b.valueOf() //"Wed Feb 27 2013 01:41:51 GMT+0300 (MSK)"
a == "Wed Feb 27 2013 01:41:51 GMT+0300 (MSK)" //true
b == "Wed Feb 27 2013 01:41:51 GMT+0300 (MSK)" //true
a == b //true
a === b //false - types are being compared as well
a.valueOf() == b.valueOf() //false - 1361918511306 compared to "Wed Feb 27 2013 01:41:51 GMT+0300 (MSK)"