1

When I assign anotherArrayList = arrayList, anotherArrayList is a pointer to arrayList. So why this does not work when I empty the arrayList.

var arrayList = ['a', 'b', 'c', 'd', 'e', 'f']; 
var anotherArrayList = arrayList;  

arrayList[1] = '15';

console.log(arrayList); // output: [ "a", "15", "c", "d", "e", "f" ]
console.log(anotherArrayList); // output: [ "a", "15", "c", "d", "e", "f" ]

arrayList = []; 
console.log(arrayList); // output: []
console.log(anotherArrayList); // output: [ "a", "15", "c", "d", "e", "f" ] 
user3351236
  • 2,488
  • 10
  • 29
  • 52
  • 7
    When you do `arrayList = [];` you aren't changing the object that `arrayList` was pointing at; you're making `arrayList` point at a new object. Your premise is wrong: "I empty the arrayList" is not what's happening. –  Feb 20 '19 at 12:14
  • @ChrisG This should be an answer. – NemoStein Feb 20 '19 at 12:17
  • 1
    Possible duplicate of [Is it possible to have 2 variables pointing to the same object? (javascript)](https://stackoverflow.com/questions/12143799/is-it-possible-to-have-2-variables-pointing-to-the-same-object-javascript) – adiga Feb 20 '19 at 12:21
  • Possible duplicate of [Are there pointers in javascript?](https://stackoverflow.com/questions/17382427) and [js replace object / change reference](https://stackoverflow.com/questions/41814414) – adiga Feb 20 '19 at 12:27
  • Does this answer your question? [Pass-by-reference JavaScript objects](https://stackoverflow.com/questions/37290747/pass-by-reference-javascript-objects) – Ivar Jun 10 '20 at 20:52

3 Answers3

2

By assigning a new array, the old object reference gets a new reference to the empty array.

You could assign zero to the length property which empties the object reference as well.

var arrayList = ['a', 'b', 'c', 'd', 'e', 'f'],
    anotherArrayList = arrayList;

arrayList[1] = '15';

console.log(arrayList);        // [ "a", "15", "c", "d", "e", "f" ]
console.log(anotherArrayList); // [ "a", "15", "c", "d", "e", "f" ]

arrayList.length = 0; 
console.log(arrayList);        // []
console.log(anotherArrayList); // [] 
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
2

It doesn't empty your other array because you're not actually clearing/emptying it, instead you are creating a new empty array in memory, which arrayList will point to, whereas anotherArrayList will still point to the full array.

Instead, to empty the array itself, you can use the .length property: arrayList.length = 0

var arrayList = ['a', 'b', 'c', 'd', 'e', 'f']; 
var anotherArrayList = arrayList;  

arrayList[1] = '15';

console.log(arrayList); // output: [ "a", "15", "c", "d", "e", "f" ]
console.log(anotherArrayList); // output: [ "a", "15", "c", "d", "e", "f" ]

arrayList.length = 0; 
console.log(arrayList); // output: []
console.log(anotherArrayList); // output: []
Nick Parsons
  • 45,728
  • 6
  • 46
  • 64
0

Array and objects are non-primitive data-type in javascript. When a variable is assigned with reference to a non-primitive data-type, the copy of memory location is stored in that new variable.

Shashank
  • 397
  • 2
  • 7
  • 17