How can you check if two or more objects/vars have the same reference?
5 Answers
You use ==
or ===
:
var thesame = obj1===obj2;
If both operands are objects, then JavaScript compares internal references which are equal when operands refer to the same object in memory.

- 372,613
- 87
- 782
- 758
-
16This works in most cases, and may work for the original poster. However, if two strings were fabricated independently and contain the same contents, then `==`, `===` and `Object.is` will all return true after performing a deep string comparison. In the strictest sense, the original poster wants the comparison to return false. I ran across an arcane situation where I also wanted that comparison to be false, and could not find a good solution. What I really need is the JS equivalents of Object.referenceEquals from .NET – bigh_29 Oct 31 '18 at 21:49
-
and how to display its reference ? – getName May 27 '19 at 12:23
-
3@getName You mean the *internal* reference ? It's managed by the implementation, is implementation defined, and isn't exported. – Denys Séguret May 27 '19 at 13:02
-
@DenysSéguret I mean the reference of an object or a primitive variable and wants to know this reference, to perform some tests, I know it can be shown in some other languages like python, but not sure it's possible on JS – getName May 28 '19 at 14:06
-
3idea: since internal reference is managed by implementation and isn't exposed we can do the following trick: we'll try to temporarily add +1 randomly named new property with random value on the 1st object and then check if expected property happen to appear on the 2nd object! Of cause we'd check if that property does not exist before adding and we'd cleanup after our intrusive check. – Dmitry Shevkoplyas May 28 '19 at 20:18
-
@bigh_29 not sure if when you wrote your comment it was like that, but I just tried now in Chrome and `new String("b") === new String("b")` returns `false`. – Luca Fagioli Feb 03 '23 at 08:10
The equality and strict equality operators will both tell you if two variables point to the same object.
foo == bar
foo === bar

- 914,110
- 126
- 1,211
- 1,335
As from ES2015
, a new method Object.is()
has been introduced that can be used to compare and evaluate the sameness of two variables / references:
Below are a few examples:
Object.is('abc', 'abc'); // true
Object.is(window, window); // true
Object.is({}, {}); // false
const foo = { p: 1 };
const bar = { p: 1 };
const baz = foo;
Object.is(foo, bar); // false
Object.is(foo, baz); // true
Demo:
console.log(Object.is('abc', 'abc'));
console.log(Object.is(window, window));
console.log(Object.is({}, {}));
const foo = { p: 1 };
const bar = { p: 1 };
const baz = foo;
console.log(Object.is(foo, bar));
console.log(Object.is(foo, baz));
Note: This algorithm differs from the Strict Equality Comparison Algorithm in its treatment of signed zeroes and NaNs.

- 37,952
- 20
- 92
- 95
-
can I edit your answer? I will add some more examples of Object.is() with Object.assign(), JSON.parse(JSON.stringify(...)) etc all – Harsh Patel Jun 06 '23 at 13:05
For reference type like objects, == or === operators check its reference only.
e.g
let a= { text:'my text', val:'my val'}
let b= { text:'my text', val:'my val'}
here a==b will be false as reference of both variables are different though their content are same.
but if I change it to
a=b
and if i check now a==b then it will be true , since reference of both variable are same now.

- 1,245
- 14
- 28
Possible algorithm:
Object.prototype.equals = function(x)
{
var p;
for(p in this) {
if(typeof(x[p])=='undefined') {return false;}
}
for(p in this) {
if (this[p]) {
switch(typeof(this[p])) {
case 'object':
if (!this[p].equals(x[p])) { return false; } break;
case 'function':
if (typeof(x[p])=='undefined' ||
(p != 'equals' && this[p].toString() != x[p].toString()))
return false;
break;
default:
if (this[p] != x[p]) { return false; }
}
} else {
if (x[p])
return false;
}
}
for(p in x) {
if(typeof(this[p])=='undefined') {return false;}
}
return true;
}

- 533
- 3
- 12
-
14The OP is asking about detecting identical references, not equality. – faintsignal Jun 27 '18 at 20:33