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?