Coyote
Coyote

Reputation: 2524

Compare if two variables reference the same object in javascript

I am looking for a way to check if two variables refer to the same object in javascript.

ex:

var a = {foo:"bar"};
var b = {foo:"bar"};
var aa = a;

testSame(a, b); //false
testSame(a, aa); //true

var r = "a string";
var s = "a string";
var rr = r;

testSame(r, s); //false
testSame(r, rr); //true

So far there doesn't seem to be a way of getting a way of doing this.

edit: testSame() is not a real function it would be the hypothetical solution.

edit: The answer to the question How to check if two vars have the same reference? does not answer this question as it uses the strict equality operator (===) which does not differentiate between between 2 vars pointing to 2 instances of an identical string.

Upvotes: 5

Views: 10246

Answers (5)

quinn
quinn

Reputation: 5998

As I understand it, === checks the identity of the object. Javascript's strings are immutable, so when you say

var x = "string"
vay y = "string"

x and y are actually referencing the same object in memory. So, unfortunately, what you want is impossible. Symbols provide this functionality:

Symbol('x') === Symbol('x') // => false

Upvotes: 0

Anton Baukin
Anton Baukin

Reputation: 61

The question is correct, but there is a trick in it. The following console code snippet reveals it:

const A = "abc", B = new String(A), C = B

> A == B
true

> A === B
false

> B === C
true

Variable A refers integral primitive that is converted to String object on a demand. JS machine may optimize references to equal primitive strings defined in the code to target exactly the same block of RAM — that's why the question, as defined, has no solution.

You need to manually wrap each integral value to corresponding object class: String, Number, Boolean, — and test them with === operator:

var r = new String("a string");
var s = new String("a string");
var rr = r;

I've applied this trick to track the initially empty controlled input in React, — whether the user had edited it and then erased to be clear, — without involving additional field in the state.

Upvotes: 3

Pointy
Pointy

Reputation: 413757

Here's how things work. First, in this code:

var a = {foo:"bar"};
var b = {foo:"bar"};

the variables a and b are initialized with references to two different objects. Thus comparisons with either == or === will report them as being different (i.e., not equal).

Here, however:

var a = "some string";
var b = "some string";

the two variables are initialized to refer to strings that are identical. String values in JavaScript are not objects — they're primitives. Comparisons between a and b with either == or === will therefore return true because the strings are the same. This is exactly the same as things would have been if the variables had been initialized like this:

var a = 17;
var b = 17;

Numbers and strings are primitives, so comparison compares the semantic values of the primitives. Objects, however, are not primitives, and comparison of objects is based on object identity.

It literally is nonsensical in the JavaScript semantic domain to want to know whether two variables pointing to the same identical string refer to two different expressions of that same string, in precisely the same way as it would be nonsensical to want to know whether one 17 were different from another.

Upvotes: 2

Stef van den Berg
Stef van den Berg

Reputation: 79

This one is already answered in this question: How to check if two vars have the same reference?

The short version use === instead of ==

Upvotes: 0

Schlaus
Schlaus

Reputation: 19212

Just comparing with == or === will do the trick, except for strings. There's no way to do what you ask with strings.

var a = {foo:"bar"};
var b = {foo:"bar"};
var aa = a;

testSame(a, b); //false
testSame(a, aa); //true

var r = "a string";
var s = "a string";
var rr = r;

testSame(r, s); // false <-- this will be true
testSame(r, rr); //true

function testSame(a, b) {
  console.log(a === b);
}

Upvotes: 4

Related Questions