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
false

benji == fido
false

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
true

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

mydog === fido
false
Related Tutorial
Follow Us
https://www.facebook.com/Rookie-Nerd-638990322793530 https://twitter.com/RookieNerdTutor https://plus.google.com/b/117136517396468545840 #
Contents +