0

I cannot find out why these lines (executed in node console) give different results...

> Array(3).map(x => '?')
[ , ,  ]
> [1,2,3].map(x => '?')
[ '?', '?', '?' ]

What does the syntax Array(3) means ?

CapelliC
  • 59,646
  • 5
  • 47
  • 90
  • MDN points out if it's an integer only passed in in the argument, it creates an array with that many items (up to that length): https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array#Syntax – Jared Farrish Jun 22 '17 at 12:06
  • while the explanation about *unassigned indexes* seems satisfactory, here is some further weirdness: `> x = Array(3) [ , , ] > x[2]=8 8 > x [ , , 8 ] ` I would expect `x` having length 4, not 3 – CapelliC Jun 22 '17 at 12:16
  • I'm guessing since it was initialized, you would have to modify the length or use `.push()` or something to add indices. – Jared Farrish Jun 22 '17 at 12:20
  • of course... [this answer](https://stackoverflow.com/a/18949651/874024) explains it all – CapelliC Jun 22 '17 at 12:22

0 Answers0