sanjar-notes / web_dev_fundamentals

The basics - HTML, CSS, Javascript and more
https://sanjar-notes.github.io/web_dev_fundamentals/
3 stars 0 forks source link

Array.fill does shallow filling #110

Open sanjarcode opened 7 months ago

sanjarcode commented 7 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 ] ]
>