Maxx Maxx - 7 months ago 38
Javascript Question

Why are two identical objects not equal to each other?

Seems like the following code should return a true, but it returns false.

var a = {};
var b = {};

console.log(a==b); //returns false
console.log(a===b); //returns false

How does this make sense?


The only difference between regular (==) and strict (===) equality is that the strict equality operator disables type conversion. Since you're already comparing two variables of the same type, the kind of equality operator you use doesn't matter.

Regardless of whether you use regular or strict equality, object comparisons only evaluate to true if you compare the same exact object.

That is, given var a = {}, b = a, c = {};, a == a, a == b, but a != c.

Two different objects (even if they both have zero or the same exact properties) will never compare equally. If you need to compare the equality of two object's properties, this question has very helpful answers.