0

I defined the following function:

const func = ()=>{
  let a =  new Array(2).fill().map(i => new Array(2).fill(1));
  console.log(`%o`,a);
  a[0][0]++;
  return a;
}

When I implement func() in DevTools, the result is this:

(2) [Array(2), Array(2)]
0: (2) [2, 1]
1: (2) [1, 1]
length: 2
__proto__: Array(0)


(2) [Array(2), Array(2)]
0: (2) [2, 1]
1: (2) [1, 1]
length: 2
__proto__: Array(0)

I want the first one of

console.log(`%o`,a); 

to be

[[1,1],[1,1]]

but the result was unexpected.

Why does this happen? Is this an example of hoisting?

Any information would be appreciated.

mplungjan
  • 169,008
  • 28
  • 173
  • 236
somia
  • 193
  • 10

0 Answers0