4

I'm currently learning arrays in JS.

Whats puzzling me is that an array like an object is a reference type.

So with that in mind if I create a new reference (variable) to the same array and empty it whether I declared it with 'const' or 'let' keywords it should always empty the array at both references.

But, if I re assign the original array using the 'let' keyword I'm allowed to empty the original array yet the one belonging to the new reference isn't emptied?

I think the code below makes it more clear:


const array1 = [1, 2, 3]
const array2 = array1

array1.length = 0;

console.log(array1) //Outputs []
console.log(array2) //Outputs []

//So both arrays are emptied which makes sense as a reference type but...

let array3 = [1, 2, 3]
let array4 = array3

array3 = []

console.log(array3) //Outputs []
console.log(array4) //Outputs [1,2,3]

//what is going on here? I thought all arrays are reference types regardless of using const or let?
Niall Parker
  • 177
  • 7
  • try same approaches - how can you tell if `array1.length = 0;` !== `array3 = []`. also, seems array3 would assign new value and array4 would keep reference in memory to [1,2,3] – andriishupta Feb 05 '20 at 21:27
  • It might be a reference type, but just like for an object, that means you need to mutate the object. Overwriting the `array3` reference with some other won't do anything to the existing array object. – Bergi Feb 05 '20 at 21:29
  • 3
    You're misunderstanding reference types. When you say `array2 = array1`, you aren't creating a reference to array1, you're creating a reference to the array *that array1 is also a reference to*. Then when you say `array3 = []` you're pointing that reference to a new array, but array4 is still pointing at the original one. – John Montgomery Feb 05 '20 at 21:29
  • 1
    @Ivar Good call, added it – Bergi Feb 05 '20 at 21:31
  • In case 1 youre mutating the array that 1 and 2 are both pointing at. In case 2 you are saying 3 now points to a new array without touching the one that 4 is pointing at. Done. – bryan60 Feb 05 '20 at 21:32
  • Sigh, thanks for your input all, i'll spend some time playing with arrays applying what you've said to wrap me head around this once and for all – Niall Parker Feb 05 '20 at 21:40

1 Answers1

6

You aren't actually emptying the original array, you are instead reassinging array3 to a brand new array. Array4 on the other hand is still pointed to the original array that array3 was created with.

When array4 was created, it is not inherently tied to the value of array3, but instead to the array that array3 was created with.

Joe Lissner
  • 2,181
  • 1
  • 15
  • 21
  • 1
    Okay i think i've got it now! because array1 and array2 both point to the same reference and because array1 is initialised as a constant I cannot re assign it from pointing to the array reference to an empty array. So because array3 is initialised using 'let' keyword when I do ```array3 = [] ``` i'm releasing it from pointing to the array reference and instead - assigning it an entirely new array while array4 still holds a reference to that original reference array3 was pointing to. Hope i've got that right, thanks a lot! – Niall Parker Feb 05 '20 at 22:36
  • That's correct, you got it! – Joe Lissner Feb 06 '20 at 18:42