22

Is there a way to know that 2 javascript variable point to the same memory address ?

var my_var = {
    id: 1,
    attribute: "myAttribute"
}

var copy = my_var;

//someting like
if(copy === my_var) return true;
Merlin
  • 4,907
  • 2
  • 33
  • 51
  • 8
    Well, you've answered your own question. :) This is true only for objects though. – freakish Jan 30 '14 at 13:19
  • `===` means that 2 vars have same type, in your case `Object` and this will be `true`. This will not check for same `memory allocation` – antyrat Jan 30 '14 at 13:20
  • It also means that they are the same object. I don't know of any JS engine which would have two separate memory allocations in such a situation. – OrangeDog Jan 30 '14 at 13:21
  • 2
    @antyrat No, `===` checks type **and** value. If both sides are objects it checks whether they point to the same address. That's it. – freakish Jan 30 '14 at 13:22
  • 1
    @antyrat It's strict equality. They have to be of the same type **and** have the same value. In the case of objects, "have the same value" is synonymous with "be the same object", which implies they occupy the same memory address(es). – Anthony Grist Jan 30 '14 at 13:22
  • So if `copy === my_var` returns true it means they both point to the same memory address. Are you 100% sure? – Merlin Jan 30 '14 at 13:25
  • @Lorenzo Yes, 100% sure. – freakish Jan 30 '14 at 13:26
  • This is correct, x = { one: 1 }, y = { one: 1 }; x === y is false, they are only equivalent if they point to the same space. For comparing objects you generally will want to compare properties on the object, libs like lodash/underscore are good for this – bendecoste Jan 30 '14 at 13:27
  • How about 2 vars are primitive type value? for example: var str1 = "foo"; var str2 = "foo"; if(str1 === str2) return true; Can we say these 2 variables point to the same memory address ? – Yang Wang Apr 07 '17 at 23:46

4 Answers4

20

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
bendecoste
  • 778
  • 6
  • 13
  • 1
    -1: In case of objects "equality" means "the same memory reference". – freakish Jan 30 '14 at 13:25
  • You are correct, removed that from the post as I think it is exaplined better above. – bendecoste Jan 30 '14 at 13:28
  • Also the first piece of code would produce the same result in C as well. Actually variable assignment works **exactly** the same as in C. The difference is in: 1) passing variables to functions, 2) there is no reference/pointer operator in JavaScript (i.e. you can't work with memory directly). – freakish Jan 30 '14 at 13:36
  • (thumbsup), I guess I should have included a C example with pointer operations, they were excluded because javascript doesn't have them .. wording is a little unclear, I suppose. – bendecoste Jan 30 '14 at 13:39
20

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
freakish
  • 54,167
  • 9
  • 132
  • 169
  • so in the first example, are x and y pointing to the same 1? please – MartianMartian Mar 05 '18 at 13:54
  • 1
    @Matian2049 As in the same memory address? That is an implementation detail and depends on the JavaScript engine you are using. For example under V8 (Chrome's JS) the answer is no: that's because primitives live on the stack. – freakish Mar 05 '18 at 13:58
  • Thanks for the prompt answer. that's what i meant, if 1 and 1 share same memo addr. that's clear now. – MartianMartian Mar 05 '18 at 14:27
  • so i'm debating w/ myself. when var a = 1; var b = a; is 1 copied to b or is be pointing to the first 1 created for a? – MartianMartian Mar 05 '18 at 14:29
  • 1
    @Matian2049 It is copied. Try this: `var a = 1; var b = a; a = 2; console.log(b);` It will print `1`. – freakish Mar 05 '18 at 14:58
  • interestingly, if a points to 1, then b points to 1, a re-points to 2, b still points to 1. looks like i have created a mirror logic world.. haha thank you for the help. – MartianMartian Mar 05 '18 at 17:05
0

In javascript when we declare more than one variable with same value then each and every variable points same memory location.Basically javascript follow the reference counting algorithm.same things happen in Python.

SwarupOVO
  • 19
  • 2
-6

In Python, you can use id(variableName) to get the memory address:

>>> x = "hello world"
>>> id(x)
4347327152
>>> x = 2
>>> id(x)
4305328544

But I don't know the how to achieve it in JavaScript. Otherwise, we can also easily see whether two pointers point to the same address. Hopefully my answer provides some useful information.

Onur A.
  • 3,007
  • 3
  • 22
  • 37
juanli
  • 583
  • 2
  • 7
  • 19
  • 7
    this is exactly what im trying to find and none of the other answers help at all! i need a javascript version of python's `id` built-in – temporary_user_name Feb 07 '19 at 00:51
  • 2
    I do not think this answer deserves such a amount of hateness. For all the people who vote down, this answer could dive to a proper search to the people who is looking for something similar in Javascript. – Luillyfe Feb 15 '20 at 16:48
  • Exactly, in python when you do `x = 1` and then `x = y`, `x` and `y` share the same reference to the memory address. I still don't understand whether in javaScript happens the same. – viery365 May 01 '20 at 19:41
  • For all the people who voted down, at-least add a reason to do so. – Himanshu Apr 21 '21 at 14:06
  • @Himanshu Clearly, this answer doesn't answer the question. "Can be done in python and not sure about Javascript" is more like an acceptable comment and not an answer. – Mathews Mathai Sep 12 '21 at 09:49
  • @Himanshu It's because the question is about JavaScript and the answer is about Python. – David Lui Jun 02 '23 at 17:06