I am working through the Splice and Slice problem and I wonder if someone could explain this. It feels like a quirk of some kind. In particular, I refer to the line: arr3.splice(n, 0, …arr1);
Why does the contents of arr2 change after this line?
Code so far:
function frankenSplice(arr1, arr2, n) {
console.log("arr1 = " + arr1);
console.log("arr2 = " + arr2);
let arr3 = arr2;
/*splice arr1 into arr3 */
console.log("arr2 before = " + arr2);
arr3.splice(n, 0, ...arr1); /*<------------???*/
console.log("arr2 after = " + arr2);
console.log("-->" + arr3);
console.log("");
return arr3;
}
The console outputs:
arr2 before = 4,5
arr2 after = 4,1,2,3