sanjar-notes / frontend

The basics - HTML, CSS, Javascript, browser APIs, performance
3 stars 0 forks source link

Array.fill does shallow filling #110

Open sanjarcode opened 9 months ago

sanjarcode commented 9 months ago
$ node
Welcome to Node.js v18.16.1.
Type ".help" for more information.
> x = Array(2).fill(Array(2).fill(0))
[ [ 0, 0 ], [ 0, 0 ] ]
> x[0][0] = 3
3
> x
[ [ 3, 0 ], [ 3, 0 ] ]
> // wtf ;(
undefined
> x = new Array(2).fill(new Array(2).fill(0))
[ [ 0, 0 ], [ 0, 0 ] ]
> x[0][0] = 3
> x
[ [ 3, 0 ], [ 3, 0 ] ]
>