4

This program:

 var arr = [];
    arr[100] = "foo";
    console.log(arr);

Outputs this:

(101) [undefined × 100, "foo"]

However, this program:

var arr = [];
for(var i = 0; i < 101; i++) {
arr[i] = (i === 100 ? "foo" : undefined);
}
console.log(arr);

Outputs this (there is a "foo" eventually, of course):

(101) [undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined…]

I could be nitpicking but I just want to make sure, why is the output different ? Shouldn't the 2 arrays be equivalent ?

Running in Chrome version 58.

doubleOrt
  • 2,407
  • 1
  • 14
  • 34
  • @ASDFGerte Yeah certainly, I just didn't know the proper terminology to google the question. – doubleOrt Sep 27 '17 at 22:15
  • in the first case the keys from 0 to 99 are not created. You can try `console.log(Object.create(arr))` in both cases to notice the difference – Slai Sep 30 '17 at 13:34

1 Answers1

5

They're not equivalent.

The first one is sparse with only a single member at index 100. The second one has 101 members, most of which are the value undefined.

A sparse array is one where there are fewer members than the .length suggests. So if you have an array where .length is 101 and all indices have a value, it's not sparse, but if there are "holes" where an actual index/value is missing, it's a sparse array.

To check if an array is sparse, you can compare its .length to the .length of the result of Object.keys.

var isSparse = arr.length !== Object.keys(arr).length;

To check if a single member is actually defined or not, use the in operator: 3 in arr Or the .hasOwnProperty() method: arr.hasOwnProperty(3)

llama
  • 2,535
  • 12
  • 11
  • This was exactly why I asked the question, I did sort of know that they were not equivalent, but what does "sparse" mean ? Could you elaborate ? When is an array sparse ? Can anything else be sparse ? – doubleOrt Sep 27 '17 at 22:12
  • 1
    Sparse means that there are fewer members than the `.length` suggests. So if you have an array where `.length` is `101`, if all indices have a value, it's not sparse, but if there are "holes" where an actual value is missing, it's a sparse array – llama Sep 27 '17 at 22:13
  • So how could one distinguish between the two if you didn't know which was sparse and which wasn't ? How could you programmatically determine which array is sparse ? – doubleOrt Sep 27 '17 at 22:17
  • You could do this to check if it's sparse or not: `arr.length === Object.keys(arr).length`. If they're equal, it's not sparse. – llama Sep 27 '17 at 22:22
  • Great, both solutions work, incorporate your comments into your answer if you want it accepted, or I will just edit it and accept it later :) – doubleOrt Sep 27 '17 at 22:26