Slice and Splice

I’m wondering why in the following code is the arr2 variable changing if i am using the slice function on the newArr2 variable?


function frankenSplice(arr1, arr2, n) {
  let newArr = [];
  
let newArr2 = arr2;

newArr = newArr2.splice(n, 0,...arr1)
console.log(arr2)
  return newArr2;
}

frankenSplice([1, 2, 3], [4, 5, 6], 1);

What do you think this line does?

Also, you are not using the slice function on anything here.

its creating a new variable to equal arr2 so i dont perform the splice on the original arr2 array.

Actually, arrays and objects assign a reference to memory to store a value. That means you assigned a reference arr2 to newArr2, so they really reference the same array. Changing one changes the other. That is why you are seeing changes in arr2 when newArr2 is spliced. You should review some of the challenges in the Basic Data Structure section and you will see how to properly make a shallow copy of arr2, which is what you thought you were doing in the line shown.

Alright i got it and used slice on the array. Yeah i still havent mastered all of the commands and should go over them again. I probably rushed through the lessons too fast. I am new to programming so all this is still pretty tricky to me.

thanks for the help