How to know that two javascript variable point to the same memory address
You can't alias variables like you can in C. In javascript, something like
var x = 1;
var y = x
y = 4;
// x is still 1
will always be the case.
However, objects are always passed by reference
var x = { one: 1, two: 2 };
var y = x;
y.one = 100;
// x.one is now 100
Is there a way to know if 2 javascript variable point to the same memory address ?
Generally the answer is no. Primitive types (like numbers) are being passed around by value. So we have
> var x = 1;
> var y = 1;
> x === y;
true
even though they don't refer to the same memory location (well, this is an implementation detail, but they are not pointing to the same memory address at least in V8).
But when both sides are objects then yes: use ==
or ===
. If both sides are objects then each operator checks whether they point at the same memory address.
> var x = {test: 1};
> var y = {test: 1};
> x === y;
false