Slice and Splice - could you explain this behavior?

Hello,
I am trying to copy the array (arr1) into the array (arr2) beginning at index (n), i’m using a for loop with assignments at the desired index (n) instead of slice and splice functions, i’m working with copies of my parameters,
However, my code seems to reassign values to both the original arrays (arr2) and it’s copy (?!!) Could you please just give me a hint on what’s going wrong,
Thank you,

function frankenSplice(arr1, arr2, n) {
  let arr2Copy = arr2;
  let arr1Copy = arr1;
  for (let i = 0; i < arr1Copy.length; i++) {
    if (i == 0) { arr2Copy[n] = arr1Copy[i]; }
    else { (arr2Copy.push(arr1Copy[i])); }
  }
  arr2Copy.push(arr2[arr2.length-1]);
  return arr2Copy;
}

They aren’t copies.

> let arr1 = [1, 2, 3]
> let arr2 = arr1
> arr1 == arr2
true

You are just referencing the arrays under a different name, they are still the same array

1 Like

Thank you, this helps