Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why are two identical objects not equal to each other?

Tags:

javascript

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?

like image 843
Maxx Avatar asked Jul 28 '12 21:07

Maxx


People also ask

Why is an object not equal to an object?

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. Objects are sometimes called reference types to distinguish them from JavaScript's primitive types.

How can you tell if two objects are identical?

To determine if two objects are identicalSet up a Boolean expression to test the two objects. In your testing expression, use the Is operator with the two objects as operands. Is returns True if the objects point to the same class instance.

How can we say that two objects are equal?

In Java, the == operator compares that two references are identical or not. Whereas the equals() method compares two objects. Objects are equal when they have the same state (usually comparing variables). Objects are identical when they share the class identity.


2 Answers

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.

like image 123
josh3736 Avatar answered Sep 24 '22 10:09

josh3736


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.

like image 27
T.J. Crowder Avatar answered Sep 25 '22 10:09

T.J. Crowder