I am working with two arrays:
let num1 = [[1]];
const num2 = [2, [3]];
When I combine these arrays, I create a new array:
const numbers = num1.concat(num2);
console.log(numbers);
// This will result in [[1], 2, [3]]
Next, I add a new value to num1:
num1[0].push(4);
console.log(numbers);
// This will result in [[1, 4], 2, [3]]
However, if I try to reassign a value to num1
like this:
num1[0] = [1, 4, 5];
console.log(numbers);
// This will result in [[1], 2, [3]]
I am puzzled as to why changing the value of num1
does not update the numbers array, while the push method does.