0

As I understand, object references x in x = { greet: hi } stores a reference to object { greet:hi} unlike primitive types that hold actual values ( y=10)

In the following code, console.log(y) outputs {greet: "hi"}.

Why does y object reference is not updated to point to {greet: 'hello'} when x obj ref is updated to point to x = {greet: 'hello'}

var x = {greet: 'hi'};
var y = x;
x = {greet: 'hello'};

console.log(y);
console.log(x)
7
  • What you describe would mean that y = x makes y and alias of x, i.e. that they're both the same variable really. That's not how it works. You're merely assigning the object reference stored in x to y. Commented Dec 20, 2017 at 8:51
  • @deceze Not sure if this is apt duplicate. Commented Dec 20, 2017 at 8:54
  • @OP, objects are assigned/copied using reference. What this means is, you are exchanging memory location and the accessing property of the object at this memory location. Hence changing property in one variable reflect in others. Now when you do x = {greet: 'hello'};, you are replacing the reference itself. So now the variable will hold pointer to some other location and object at previous location would remain as is. If no one is referencing it, GC will get rid of it. Commented Dec 20, 2017 at 8:57
  • @deceze Sure, I checked the referred question. Although there are some commonalities regarding the nature of both question but they aren't exactly same. Commented Dec 20, 2017 at 8:57
  • @Rajesh Meh… at the very least this question definitely gets answered several times a month, and we don't need yet another copy of it. If you have a better suggestion for a dupe target, by all means… Commented Dec 20, 2017 at 8:58

2 Answers 2

1

Because in line x = {greet: 'hello'}; a new object is being created.

Use x.greet = 'hello'; to update old object.

var x = {greet: 'hi'};
var y = x;
x.greet = 'hello';

document.getElementById("text_show").innerHTML= x.greet + " | " + y.greet;
<div id="text_show"></div>

See here to know about objects in JavaScript

Sign up to request clarification or add additional context in comments.

1 Comment

Indeed. Basically, you'd see what you expect (@OP), if you'd change x by saying x.greet = "hello". In this case y would look like x
0

Why does y object reference is not updated to point to {greet: 'hello'} when x obj ref is updated to point to x = {greet: 'hello'}

Because you have assigned a new reference to x while y is still pointing to original one.

When you say

var y = x;

you are pointing y to the same reference that x was pointing to, you are not pointing y to x.

3 Comments

you are not pointing y to x. exactly, x is a reference and that reference is also copied to y when I write var y = x so when that reference changes in x that change should propagate to y also. Right?
so when that reference changes in x that change should propagate to y also No, if the reference value itself has changed then x will point to a new reference (new object) while y will keep pointing to older one. If the value is changed for example var x = {greet: 'hi'}; var y = x; x.greet = 'hello'; then y.greet would also have changed.
Found a more direct explanation here. > Javascript's evaluation strategy is to always use call by value, but in the case of Objects (including arrays) the value passed is a reference to the Object, which is not copied or cloned. If you reassign the Object itself in the function, the original won't be changed, but if you reassign one of the Object's properties, that will affect the original Object.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.