Learn JavaScript
Copying Objects
37.3
Lesson

Comparing Objects

When we compare two similar-looking objects stored in different variables, we see that they are not equal.

In the example given below, the variables user and anotherUser store a similar-looking object. But the result of their comparison is false.

The result is false because they are stored in different locations in the memory. When we check for their equality, we are actually comparing the reference of the objects stored in the variables.

However, when we compare two variables that reference the same object, then the result is true.

In the example given below, the variables user and anotherUser reference the same object. Therefore, the result of their comparison is true.