3

In javascript, when creating a new Array of length 3, I cannot seem to wrap my head around why this works:

[...Array(3)] // [undefined, undefined, undefined]

But this won't work:

Array(3).map(e => undefined) // [ , , ]

As I understood correctly here, Array(3) will return an array with 3 empty slots, which is not the same as [undefined, undefined, undefined], but why can you not iterate over them with map but the spread operator can?

FZs
  • 16,581
  • 13
  • 41
  • 50
dreadnaught
  • 138
  • 8

0 Answers0