0

I assumed these would be equal:

new Array(1).fill(0); // [0]

new Array(1).map(x => 0); // [undefined]

Why doesn't map work in the second case?

Leo Jiang
  • 24,497
  • 49
  • 154
  • 284
  • Because `new Array(1)` is an array with _one empty index_. The array has a length of 1 but it doesn’t have the property `0`, so there’s nothing to iterate over. – Sebastian Simon Mar 01 '16 at 07:21
  • It does work. It just doesn't do what you think it does, because `map` skips "holes". –  Mar 01 '16 at 07:21
  • You can refer polyfill on following post [MDN - Array.map](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map) for more information – Rajesh Mar 01 '16 at 07:24

0 Answers0