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.
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.
equals() versus == The == operator compares whether two object references point to the same object. For example: System.
Objects are not like arrays or strings. So simply comparing by using "===" or "==" is not possible. Here to compare we have to first stringify the object and then using equality operators it is possible to compare the objects.
You just need this
if(c == a) { // same instance }
a == b
and b == c
will return false
Just a standard equality test:
( a == c ) // true ( a == b ) // false
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With