Because objects are compared by reference:
- Functions
- Objects (Host objects, native objects/constructors and instances)
- A common instance is
{}
, which is similar to new Object
.
The following object types are compared by value, not by reference:
- Strings
- Numbers
- Booleans
null
and undefined
Additionally, there's one object which is never equal to itself, not even by reference:
var test = NaN;
alert ( test == NaN ); // false
alert ( test == test ); // false (!)
To check whether two objects are equal, you have to define equality:
- "Two objects are equal if they contain the same property names and values"
Which implies that object A has to have the same number of properties as object B, and that every property in A has also to be a property of B.