2

new Array(3)returns an array of length 3 containing 3 undefineds which is equivalent to [undefined, undefined, undefined];

However,

[undefined, undefined, undefined].map((val, i) => i) produces the expected result of [0, 1, 2]. But new Array(3).map((val, i) => i) produces [undefined, undefined, undefined], as if the map function had not effect whatsoever.

Could anyone explain why?

EDIT Looks like there is a flaw in my understanding of new Array(). It does NOT create a new array. It creates an object with key length equal to the argument passed in. Thanks for the answers and comments.

Btw if you do need an array like [undefined, undefined, undefined] to iterate/map over, or for anything then [...new Array(m)] should do the trick.

Pratheep
  • 936
  • 1
  • 7
  • 17
  • 3
    *containing 3 undefineds which is equivalent to [undefined, undefined, undefined];* That's wrong. –  Jun 14 '17 at 16:50
  • Arrays in Javascript are exotic objects with numeric properties and a `length` property. In the case of `new Array(3)`, you generate an `Array` object with its `length` set to 3, but none of the numeric properties. You can see this: > Object.keys( new Array( 3 ) ); [] > Object.keys( [ undefined, undefined, undefined] ); ["0", "1", "2"] You can achieve what you want with [`Array.from`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/from?v=control): > Object.keys( Array.from( { length: 3 } ) ); ["0", "1", "2"] – error Jun 14 '17 at 16:56
  • Doh. Locked before I could post my answer. – error Jun 14 '17 at 16:56

3 Answers3

3

new Array(3)returns an array of length 3 containing 3 undefineds which is equivalent to [undefined, undefined, undefined];

Not according to MDN:

this implies an array of arrayLength empty slots, not slots with actual undefined values

Hugues M.
  • 19,846
  • 6
  • 37
  • 65
3

when you do new Array(3) Javascript reserves memory for an Array of 3 elements, but does not define any of them.

When you do create an array via [undefined, undefined undefined] it actually creates 3 elements which value is undefined each of them (therefore, it is an array of length 3).

Mayday
  • 4,680
  • 5
  • 24
  • 58
3

An important thing to understand that there is no array type in javascript. Built-in Array is just a convenience wrapped over the standard Object. The only difference is that Arrays have the length property, computed in a special way.

new Array(3) returns an object with a single length field. It doesn't contain any other keys.

{
   length: 3
}

[undefined, undefined, undefined] creates an object with 3 numeric slots:

{
   length: 3
   0: undefined,
   1: undefined,
   2: undefined,
}

That makes difference, because map and other iterators look for numeric keys that actually exist in the object. The logic behind map, forEach and friends is like this:

  for (var i = 0; i < A.length; i++)
      if (A.hasOwnProperty(i))
          do something with A[i]
georg
  • 211,518
  • 52
  • 313
  • 390