0

Probably noob JS question, but when we try to do this

new Array(5).map((_, index) => index);

we could expect to get something like 0,1,2,3,4 but we get an array of 5 empty elements. Why is that?

Valentin Kuzub
  • 11,703
  • 7
  • 56
  • 93
  • 1
    The issue here is that `new Array(5)` does not have any numerically-named properties. It only has a `length` property with the value `5`, but it has no properties named `0`, `1`, `2`, `3`, or `4`, as would exist on an array with 5 actual values. Since those properties don't exist, they are not handled by `map`. – apsillers Apr 19 '18 at 15:53
  • @apsillers thanks, appreciated – Valentin Kuzub Apr 19 '18 at 16:08
  • `Array.from(Array(5), (_, index) => index);` –  Apr 19 '18 at 16:26

0 Answers0