How to test same object instance in Javascript?

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.

Answers:

Answer

You just need this

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

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

Answer

Just a standard equality test:

( a == c ) // true
( a == b ) // false
Answer

I know the question is about checking if two objects are the same instance but this thread will not be complete without the following.

If you are after checking whether 2 objects are the same, a double equal == is enough. However, for value types (primitives), you may be in a for surprise. Check out the following:

var a = 1;  // Integer 1
var b = '1' // String '1'
if (a == b) console.log('Same.');
else console.log('Not same.');

Above will print Same.. To avoid that, use the triple equal === which means two things:

  1. Are they the same value?
  2. Are they the same type?

Or you can use Object.is method like Object.is(a, b).

if (a === b) console.log('Same.');
else console.log('Not same.');
if (Object.is(a, b)) console.log('Same for Object.is.');
else console.log('Not same for Object.is.'); 

Above will print Not same. and Not same for Object.is..

Some More Info

Below is some more information which has been copy/pasted from this article:

Which operation you choose depends on what sort of comparison you are looking to perform. Briefly:

  • double equals (==) will perform a type conversion when comparing two things, and will handle NaN, -0, and +0 specially to conform to IEEE 754 (so NaN != NaN, and -0 == +0);

  • triple equals (===) will do the same comparison as double equals (including the special handling for NaN, -0, and +0) but without type conversion; if the types differ, false is returned.

  • Object.is does no type conversion and no special handling for NaN, -0, and +0 (giving it the same behavior as === except on those special numeric values).

Note that the distinction between these all have to do with their handling of primitives; none of them compares whether the parameters are conceptually similar in structure. For any non-primitive objects x and y which have the same structure but are distinct objects themselves, all of the above forms will evaluate to false.


Conclusion

My two cents on this would be to get into the habit of always using triple equals === because it cannot hurt you but double equals == can indeed hurt you. You may use double equals == when you just care about the value and the thing you are comparing are primitives; but even this is discouraged because it mostly means a flaw in design. Languages like TypeScript will even avoid that i.e. comparing a '1' to 1 without being explicit about it.

Tags

Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.