0
//create an array of length 2 with an empty array at every index
var arr = Array(2).fill(Array());

console.log(arr);
// output: [ [], [] ]

console.log(arr[0]);
// output: []

// push string to inner array at index 0 
arr[0].push("a string");

console.log(arr);
// output: [ [ 'a string' ], [ 'a string' ] 

When I log arr[0] the output is []... If arr[0] refers to a single inner array, why does arr[0].push("a string") affect both arrays?

MaxB
  • 15
  • 4

0 Answers0