Why are two identical objects not equal to each other?

Javascript

Javascript Problem Overview


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?

Javascript Solutions


Solution 1 - Javascript

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.

Solution 2 - Javascript

> How does this make sense?

Because "equality" of object references, in terms of the == and === operators, is purely based on whether the references refer to the same object. This is clearly laid out in the abstract equality comparison algorithm (used by ==) and the strict equality comparison algorithm (used by ===).

In your code, when you say a==b or a===b, you're not comparing the objects, you're comparing the references in a and b to see if they refer to the same object. This is just how JavaScript is defined, and in line with how equality operators in many (but not all) other languages are defined (Java, C# [unless the operator is overridden, as it is for string], and C++ for instance).

JavaScript has no inbuilt concept of equivalence, a comparison between objects that indicates whether they're equivalent (e.g., have the same properties with the same values, like Java's Object#equals). You can define one within your own codebase, but there's nothing intrinsic that defines it.

Solution 3 - Javascript

As from The Definitive Guide to Javascript.

Objects are not compared by value: two objects are not equal even if they have the same properties and values. This is true of arrays too: even if they have the same values in the same order.

var o = {x:1}, p = {x:1};  // Two objects with the same properties
o === p                    // => false: distinct objects are never equal 
var a = [], b = [];        // Two distinct, empty arrays 
a === b                    // => false: distinct arrays are never equal 

Objects are sometimes called reference types to distinguish them from JavaScript’s primitive types. Using this terminology, object values are references, and we say that objects are compared by reference: two object values are the same if and only if they refer to the same underlying object.

var a = {};   // The variable a refers to an empty object. 
var b = a;    // Now b refers to the same object. 
b.property = 1;     // Mutate the object referred to by variable b. 
a.property          // => 1: the change is also visible through variable a. 
a === b       // => true: a and b refer to the same object, so they are equal. 

If we want to compare two distinct objects we must compare their properties.

Solution 4 - Javascript

use JSON.stringify(objname);

var a = {name : "name1"};
var b = {name : "name1"};

var c = JSON.stringify(a);
var d = JSON.stringify(b);

c==d;
//true

Solution 5 - Javascript

Here is a quick explanation of why {} === {} returns false in JavaScript:

From MDN Web Docs - Working with objects: Comparing objects.

> In JavaScript, objects are a reference type. Two distinct objects are never equal, even if they have the same properties. Only comparing the same object reference with itself yields true.

// Two variables, two distinct objects with the same properties
var fruit = {name: 'apple'};
var fruitbear = {name: 'apple'};

fruit == fruitbear; // return false
fruit === fruitbear; // return false
// Two variables, a single object
var fruit = {name: 'apple'};
var fruitbear = fruit;  // Assign fruit object reference to fruitbear

// Here fruit and fruitbear are pointing to same object
fruit == fruitbear; // return true
fruit === fruitbear; // return true

fruit.name = 'grape';
console.log(fruitbear); // output: { name: "grape" }, instead of { name: "apple" }

> For more information about comparison operators, see Comparison operators.

Solution 6 - Javascript

===, the strictly equal operator for objects checks for identity.

> Two objects are strictly equal if they refer to the same Object.

Those are two different objects, so they differ.

Think of two empty pages of paper. Their attributes are the same, yet they are not the same thing. If you write something on one of them, the other wouldn't change.

Solution 7 - Javascript

> How does this make sense?

Imagine these two objects:

var a = { someVar: 5 }
var b = { another: 'hi' }

Now if you did a === b, you would intuitively think it should be false (which is correct). But do you think it is false because the objects contain different keys, or because they are different objects? Next imagine removing the keys from each object:

delete a.someVar
delete b.another

Both are now empty objects, but the equality check will still be exactly the same, because you are still comparing whether or not a and b are the same object (not whether they contain the same keys and values).

Solution 8 - Javascript

This is a workaround: Object.toJSON(obj1) == Object.toJSON(obj2)

By converting to string, comprasion will basically be in strings

Solution 9 - Javascript

In Javascript each object is unique hence `{} == {}` or `{} === {}` returns false. In other words Javascript compares objects by identity, not by value.

 1. Double equal to `( == )` Ex: `'1' == 1` returns true because type is excluded 
     
 2. Triple equal to `( === )` Ex: `'1' === 1` returns false compares strictly, checks for type even

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMaxxView Question on Stackoverflow
Solution 1 - Javascriptjosh3736View Answer on Stackoverflow
Solution 2 - JavascriptT.J. CrowderView Answer on Stackoverflow
Solution 3 - JavascriptCharlie LynchView Answer on Stackoverflow
Solution 4 - JavascriptChristinaView Answer on Stackoverflow
Solution 5 - JavascriptMarian13View Answer on Stackoverflow
Solution 6 - JavascriptKaroly HorvathView Answer on Stackoverflow
Solution 7 - JavascriptMatt WayView Answer on Stackoverflow
Solution 8 - JavascriptAnze JarniView Answer on Stackoverflow
Solution 9 - JavascriptSukshith SView Answer on Stackoverflow