0

I would like to understand about this particular behaviour of Javascript. I have an object defined in the parent scope.

In a nested scope, I created a variable which references a particular object attribute (an array) of my object. Using this variable (a reference) I updated the attribute of the object. But post the nested block, I find that my update was lost?

I tried to wrap my head around this but I'm stuck.

I would think that the variable inside the nested block myOldRef should be able to access the object defined in the parent block and hence should be able to update it via reference? Also how is the value being "set back" to the older value? Is Javascript creating a copy of the object inside the nested block?

Am I missing something? Would like to know the intricacies of this behaviour.

Here is the code in question.

var a = {
  a: 'Hello',
  b: 'World',
  c: {
    mList: [0,1,2,3]
  }
}

let myRef = a['c']['mList'];
let myOldRef;

console.log(`First time: ${myRef}`);

myRef = [4,5,6,7];

console.log(`After first update: ${myRef}`);

if(true) {
  myOldRef = a['c']['mList'];
  let myNewRef = [3,4,5,6];
  myOldRef = myNewRef;
  console.log(`Inside nested block, After update: ${myOldRef} -- why is this update lost?`);
}


console.log(`Outisde nested block, After update: ${myRef}`);

You can also run the code here on replit if you'd like.

Arijit Kanrar
  • 451
  • 3
  • 15
  • 1
    This line: `myRef = [4,5,6,7]` creates an array literal and then assigns `myRef` to point to it. Any previous assignment was lost. Why do you think the `myOldRef` is lost? You never log it. Also, please read [this `let vs var` answer](https://stackoverflow.com/a/11444416/2487517) – Tibrogargan May 02 '22 at 18:47
  • are you expecting `myRef = [4,5,6,7]` to assign the array to the `c.mList` property? That's not how variable assignment works. You could however assign `c` to a variable and then update `mList`. `let myRef = a.c; myRef.mList = [4,5,6,7];` and now `a.c.mList` will be updated. – pilchard May 02 '22 at 18:49
  • (Voting to close: typo) Your last line probably was supposed to be `console.log(\`Outisde nested block, After update: ${myOldRef}\`);`. The reference is not lost. – Tibrogargan May 02 '22 at 18:53
  • I can't tell if you're confused about variable scope, or how javascript passes references/values. (or if you've just made a typo as @Tibrogargan suggested) – pilchard May 02 '22 at 18:58
  • Thanks @Tibrogargan & @pilchard. I guess I do understand variable scope rules and how let works vs var. I'm actually confused about variable assignment as @pilchard pointed out. I was expecting `myRef` and `myOldRef` to point to the same property i.e. the 'a.c.mList' property. So any update made would be reflected by anyone logs it. So I expected `myRef` to show the updates peformed via the `myOldRef`. – Arijit Kanrar May 03 '22 at 05:02
  • All object assignments in JavaScript are by reference. `[ 1 ]` is an Object (an Array with one element. If you do `x = [ 1 ]`, x refers to the Array. If you then do `x = [ 2 ]`, x will refer to the new Array Object. If nothing else refers to the original Array it is gone, and will be garbage collected. – Tibrogargan May 03 '22 at 06:04

0 Answers0