Comparing Objects

When you compare objects, it will return true only if you compare two references to the same object. Comparing two distinct objects which have the exact same methods and properties will return false.

Let's create two objects that look the same:

var fido = {breed: 'dog'};

var benji = {breed: 'dog'};

Comparing them will return false:

benji === fido

benji == fido

You can create a new variable mydog and assign one of the objects to it, this way mydog actually points to the same object.

var mydog = benji;

In this case benji is mydog because they are the same object (changing mydog's properties will change benji's). The comparison returns true.

mydog === benji

And because fido is a different object, it does not compare to mydog:

mydog === fido
Related Tutorial
Follow Us #