4
var array = [,, "cat", "dog"];

This will create an array with two empty values (so that I can use them later on). I can't think of any other way to do this without doing the slow and tedious array[2] = "cat"; array[3] = ... Is this a good way to do this. Or is there another recommended practice that emulates this ability.

David G
  • 94,763
  • 41
  • 167
  • 253

3 Answers3

4

Yes. The holes will not be defined, but contribute to the Array length.

The spec [ref] allows it:

Array elements may be elided at the beginning, middle or end of the element list. Whenever a comma in the element list is not preceded by an AssignmentExpression (i.e., a comma at the beginning or after another comma), the missing array element contributes to the length of the Array and increases the index of subsequent elements. Elided array elements are not defined.

However, as pointed by pimvdb, the fact that it's possible does not necessarily imply that it's a good practice.

Arnaud Le Blanc
  • 98,321
  • 23
  • 206
  • 194
  • 1
    The fact that it's possible does not necessarily imply that it's a good practice, IMO. – pimvdb Sep 03 '11 at 20:14
  • 1
    You could always add comments of some kind to clarify that the holes are intentional: `array=[ /* 0 */, /* 1 */, "cat", "dog" ]` – user113716 Sep 03 '11 at 20:20
  • ...or just use an object, where you won't even need to incorporate bizarrely "blank" elements - just have the numeric keys you want mapped to values, and add new keys later when needed. – karim79 Sep 03 '11 at 20:22
  • 3
    Note that as the spec. denotes, the elements will be *not defined*, which is a [bit different than being `undefined`](http://stackoverflow.com/q/3420071#3420160), the corresponding numeric properties will not exist, e.g.: `'0' in [,]; // false` (With the exception of IE<=8, that has *several* bugs on the Array Initialiser syntax) – Christian C. Salvadó Sep 03 '11 at 20:48
3

The code at firsts looks like an error.

If you really want undefined values it would be beneficial to make it absolutely clear.

var array = [undefined,undefined, "cat", "dog"];

Not really better, but you could also do the following.

var array = ["cat", "dog"];
array.unshift(undefined,undefined);

It's definitely valid but not necessarily dry and clear.

Lime
  • 13,400
  • 11
  • 56
  • 88
  • 2
    `array.unshift(undefined,undefined)` would also work since you can pass multiple arguments, just like `.push()`. – user113716 Sep 03 '11 at 20:14
1

Yeah, it's fine. Essentially what you're doing is declaring an array of four elements but without having to type in undefined for the first two. If, however, you don't want the length to equal four, you will have to do it another way.

It seems that your array is a list of elements where the mapping of the index to the value is important. If that is the case, you might consider using an object instead, which (I think) would be neater:

var obj = { 2: "foo", 3: "bar"};
alert(obj[2]);
// later
obj[4] = "baz";
alert(obj[4]);
karim79
  • 339,989
  • 67
  • 413
  • 406