0

With Javascript, if I have:

var a = {'x':1, 'y':2};
var b = a;

console.log(a);
console.log(b);

...then both a and b will print the same thing. My understanding is that both a and b are pointing to the same data in memory. However, if I do:

a = {};
console.log(b);

...then b still has the original object, even though a is an empty object now. I'd like to be sure that there won't be duplicate copies of the object in memory if I set a variable like above e.g. var b = a. In some languages, giving a new value to a would have a knock-on effect to b, since they both point to the same data in memory.

I'm confused by the fact that setting a = {} doesn't also set b to an empty object. Are they actually pointing to the same data until I set a = {}?

RTF
  • 6,214
  • 12
  • 64
  • 132
  • Eh, if a == b then we are talking about copied data, or am I misunderstanding you? – B001ᛦ Aug 16 '16 at 12:02
  • To be honest, I think you've answered your own question... – timothyclifford Aug 16 '16 at 12:03
  • Well I suppose so, but I'd just like to be sure, I was hoping someone would with understanding of Javascript engines or having more experience with the language than me could confirm. As I said, the fact that setting `a = {}` doesn't set `b` to the same is confusing me. – RTF Aug 16 '16 at 12:05
  • Depends. if you are referencing an array, function, string literal or object, then it doesn't though it still takes the memory for a pointer which is in fact what the first variable does as well..... so it kinda does take up twice the space in memory, however the space is not based on the SIZE of the object, it is always just a pointer.... if we are talking about things such as numbers which are not passed by reference, then it takes more memory. – Dellirium Aug 16 '16 at 12:06

4 Answers4

3

Below 3 images will give you whats happening here enter image description here

enter image description here

enter image description here

Oxi
  • 2,918
  • 17
  • 28
1

It's pass-by-value, but for objects, the value of the variable is a reference.

(Source: https://stackoverflow.com/a/5314911/734525, Tim Goodman)

This question has been answered many times, you can find a great explanation here

& also, check with a quick JSFiddle.

var a = {"test": 1};
var b = a;

console.log(a);
console.log(b);

a.test = 2;

console.log(a);
console.log(b);

a = {"new_test": 3};

console.log(a);
console.log(b);
Community
  • 1
  • 1
nioKi
  • 1,259
  • 9
  • 17
0

That gif simply explains how it works. Your case is the second case, passing by value :)

  • 2
    While linking to external references is nice in answers, the answer itself should contain all the relevant or quoted information from the link so that the answer is self contained. – crashmstr Aug 16 '16 at 12:08
  • It is passing by reference, not value. I am in a mild state of shock and trying not to sound rude at the moment. – Dellirium Aug 16 '16 at 12:08
0

In your case NO only when you use primitive values such as (string, number, boolean..) then when you set b=a both variables are siting in separate memory address. When you use objects then you use by reference that means that both variables points to same spot in memory.

Don't be afraid to use Chrome dev tools to experiment these type of things...it's best practice for learning JavaScript...

Enis Jasarovic
  • 125
  • 1
  • 8