7

Why this:

[undefined, undefined].map(function(i) { console.log(i); })

produces the expected output (2 times undefined), but this:

(new Array(2)).map(function(i) { console.log(i); })

doesn't?

Misha Moroshko
  • 166,356
  • 226
  • 505
  • 746
  • 4
    What is the difference between assigning undefined to a property, and not setting a property at all? This is the same difference as `Array(n)` does not assign property values. There are definitely duplicates, especially wrt. `Array.map` / `Array.forEach` behavior (tldr; they iterate only *assigned* elements). – user2864740 Aug 31 '15 at 06:38
  • 2
    http://stackoverflow.com/questions/23460301/foreach-on-array-of-undefined-created-by-array-constructor?rq=1 , http://stackoverflow.com/a/9329476/2864740 (specific answer) , http://stackoverflow.com/questions/931872/what-s-the-difference-between-array-and-while-declaring-a-javascript-ar?lq=1 – user2864740 Aug 31 '15 at 06:43

0 Answers0