In your example, a
and b
are two different objects of the same type, potentially with the same value, but the equality operators recognize that they are different instances and as such are not considered equal. The spec covers exactly how that works.
If you want to compare them, you need to compare the values within the objects. Calling getTime()
on both will give you numbers, which can be safely compared. There's also a chance that the two objects don't have the same value (allocated just as the clock ticks), so getTime()
may not always be true.
var a = new Date();
var b = new Date();
console.log(a == b); // Always false, a and b are different instances
console.log(a.getTime() == b.getTime()); // Usually true, since getTime returns a number