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;
9
  • 8
    Well, you've answered your own question. :) This is true only for objects though. Commented Jan 30, 2014 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 Commented Jan 30, 2014 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. Commented Jan 30, 2014 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. Commented Jan 30, 2014 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). Commented Jan 30, 2014 at 13:22

4 Answers 4

21

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
Sign up to request clarification or add additional context in comments.

4 Comments

-1: In case of objects "equality" means "the same memory reference".
You are correct, removed that from the post as I think it is exaplined better above.
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).
(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.
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

6 Comments

so in the first example, are x and y pointing to the same 1? please
@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.
Thanks for the prompt answer. that's what i meant, if 1 and 1 share same memo addr. that's clear now.
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?
@Matian2049 It is copied. Try this: var a = 1; var b = a; a = 2; console.log(b); It will print 1.
|
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.

1 Comment

Hw to proof each and every variable point same memory location by a program?
-5

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.

6 Comments

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
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.
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.
For all the people who voted down, at-least add a reason to do so.
@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.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.