0

Not sure how else to explain the question. Here is some very simple code to demonstrate it though.

I would expect testValue and testValue2 to be [20, 21, 22], since they are passed by reference and not passed by value, as far as I understand it, and as far as it looks like since they update with the .push() method.

Does anyone know what is happening here? Why do both testValue and testValue2 seem to start passing by value after we reassign the original array?

let testArray = [1, [10, 11, 12], 3, 4, 5];
let testValue = testArray;

testArray.push(13);
testArray = [20, 21, 22];

console.log(testArray);
console.log(testValue);


//Different attempt
let testArray2 = [1, [10, 11, 12], 3, 4, 5];
let testValue2 = testArray2[1];

testArray2.push(13);
testArray2[1] = [20, 21, 22];

console.log(testArray2);
console.log(testValue2);
Andreas
  • 21,535
  • 7
  • 47
  • 56
Static
  • 1
  • Just for clarification: _"since they are passed by reference and not passed by value"_ - JavaScript is always pass by value. But in case of objects its not the object itself, it's a "pointer" to the object: [Is JavaScript a pass-by-reference or pass-by-value language?](https://stackoverflow.com/questions/518000/is-javascript-a-pass-by-reference-or-pass-by-value-language) – Andreas Nov 24 '21 at 08:38
  • `testValue` has a pointer to the array `[1, [10, 11, 12], ...]`, `testArray` a pointer to the array `[20, 21, 22]`. `testArray2` stores a pointer to `[1, [10, 11, 12], ... ]` and `testValue2` a pointer to `[10, 11, 12]` (the one _in_ `testArray2`). You then only modify the array that `testArray2[1]` points to _in_ `testArray2`, but not the content of `testValue2`. – Andreas Nov 24 '21 at 08:49
  • Imho the question in my "clarification" comment would make a good dupe target... – Andreas Nov 24 '21 at 08:50
  • Thanks for the comment! "You then only modify the array that testArray2[1] points to in testArray2, but not the content of testValue2" Yes but since testValue2 points to specifically the array within testArray2, shouldn't it change as well? And why not? They are pointing to the same array, right? – Static Nov 24 '21 at 08:59
  • No. `testValue2` points to (is a reference that points to) `A`. You then replace the "array" (its pointer) in `testArray2` with `B`. But that doesn't change the content of `testValue2` which still points to (is a reference that points to) `A`. – Andreas Nov 24 '21 at 10:58

1 Answers1

0

problem: location memory.

you create new variable testValue with testArray, this 2 variable have same location memory, then you use .push() it will push new value to location memory of variable, not depend of the array you use it.

if you not want they same location memory. use this:

// Array.from
let testArray = [1, [10, 11, 12], 3, 4, 5];
let testValue = Array.from(testArray);

// spread operator
let testArray = [1, [10, 11, 12], 3, 4, 5];
let testValue = [...testArray];

for more detail check this article:

  1. https://backbencher.dev/articles/javascript-variables
  2. https://medium.com/@ethannam/javascripts-memory-model-7c972cd2c239