I've been racking my brain trying to figure out why these arrays are syncing after I assign one to the other. The output should be "1, 2, 3, 4" but instead it's "5, 6, 7, 8". Do I need to copy the arrays differently?
var firstArray = [1, 2, 3, 4];
var secondArray = [5, 6, 7, 8];
for (i = 0; i < firstArray.length; i++) {
var myTempArray = firstArray;
myTempArray[i] = secondArray[i];
}
console.log("Result: " + firstArray);
Expected output:
Result: 1,2,3,4
Actual output:
Result: 5,6,7,8
How do I alter the second array without changing the first array?

myTempArrayandfirstArrayboth reference the same array. In JavaScript, objects (including arrays) are reference values.var myTempArray = firstArray.slice()- this should be done BEFORE the loopvar myTempArray = [...firstArray];Using a Spread will create a copy, not a reference.:D