Why is the value of array2 [[], [], [], [], []] at the end of the loop?
var array1 = [];
var array2 = [];
for (let i = 1; i <= 10; i++) {
array1.push(i);
if (i % 2 === 0) {
//console.log(array1);
array2.push(array1);
array1.length = 0;
};
};
console.log(array1);
console.log(array2);
Can anyone explain, what's going on in this code?
.lengthofarray1to zero. Pushing the array into another array does not make a copy.