How to test same object instance in Javascript?

Jérôme Verstrynge picture Jérôme Verstrynge · Oct 6, 2014 · Viewed 13.3k times · Source

Say I have the following objects in Javascript:

var a = { xxx: 33 };
var b = { xxx: 33 };
var c;

c = a;

What is the Javascript test that will tell me whether I am dealing with the same object instance? In other words, it should return false for a and b, b and c, but true for a and c.

Answer

Amit Joki picture Amit Joki · Oct 6, 2014

You just need this

if(c == a) {
   // same instance
}

a == b and b == c will return false