0

If I create the following constant in the browser console:

const myArray = Array(2);

This outputs the following:

(2) [empty × 2]

And when I use the spread operator in the following manner:

const myUndefinedArray = [...myArray]

Then the console returns:

(2) [undefined, undefined]

In both cases it says the length is two. What are the use cases for each of these options? Is there any specific implication to "empty"?

nicokruk
  • 59
  • 6
  • Your second one has converted a sparse array into a normal one. IOW: the second one is going to take memory, the first one doesn't take memory until a element is used. IOW: Array elements 0 & 1, actually have a value, and it's value is undefined. – Keith Feb 04 '19 at 14:02
  • The first is an empty array with length 2, the second is an array with 2 elements which happen to be the value `undefined` – Andreas Feb 04 '19 at 14:02
  • @Keith Andreas that's actually not true. Array(2) will create an array with two undefined elements within it. Try it and log it. – kemicofa ghost Feb 04 '19 at 14:03
  • 2
    Possible duplicate of [What's the difference between empty items in a JavaScrip array and undefined?](https://stackoverflow.com/questions/50326047/whats-the-difference-between-empty-items-in-a-javascrip-array-and-undefined) and [Difference between “empty' and ”undefined" in Javascript?](https://stackoverflow.com/questions/51936008/) – adiga Feb 04 '19 at 14:03
  • @kemicofa In Chrome it shows like the OP says. – Keith Feb 04 '19 at 14:04
  • @Keith, weird. I'm using chrome ( Version 71.0.3578.98 (Official Build) (64-bit) ) and it's showing me double undefined. – kemicofa ghost Feb 04 '19 at 14:05
  • @kemicofa I'm same version, so it is very strange.. ps. If you access a sparse array element, it returns undefined. That's because `empty` is not a Javascript type, it's just how Chrome represents un-allocated sparse array elements. – Keith Feb 04 '19 at 14:11
  • @kemicofa _"Array(2) will create an array with two undefined elements within it"_ - No, it doesn't: ECMASCript 2016 -> [22.1.1.2 `Array(len)`](https://www.ecma-international.org/ecma-262/7.0/#sec-array-len) -> Steps 7 and 8 – Andreas Feb 04 '19 at 15:33

4 Answers4

1

main difference would be forEach and map properties

Array(2).map(()=>"value") will not do anything

[undefined,undefined].map(()=>"value") will map

Krzysztof Krzeszewski
  • 5,912
  • 2
  • 17
  • 30
0

The empty values aren't iterable:

var arr = new Array(5)

arr.forEach(()=> console.log('hello'))

var arr2 = [...arr]

arr2.forEach(()=> console.log('world'))
James Coyle
  • 9,922
  • 1
  • 40
  • 48
0

Empty in an array means the index has never been assigned a value. This has to do with how Javascript deals with arrays. You can try this:

var v = new Array();   
v[0]=undefined;
v.length = 2

console.log(v);

See, array index 0 has a "property" with name "0", without a value assigned to it (i.e. undefined). The array, however, does not have a property with name "1" at this point. Chrome logs this as "empty" value.

Marcio Lucca
  • 370
  • 2
  • 10
0

const myArray = Array(2);
console.log(myArray)

You're creating here an empty with length 2, that's why you're getting the output of an array with 2x empty values as you didn't put any values inside the created array.

const myArray = Array(2);
const myUndefinedArray = [...myArray]
console.log(myUndefinedArray)

You get here undefined because you're trying to create an myUndefinedArray array using nonexistent values.

Alexandre Elshobokshy
  • 10,720
  • 6
  • 27
  • 57