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.

3
  • 1
    Why you need such array? Commented Sep 3, 2011 at 20:09
  • There was one time where I needed to put something into the front of a pre-filled array. I forgot what it was though but it brought up this question. Commented Sep 3, 2011 at 20:14
  • You could use then array.unshift(value) Commented Sep 3, 2011 at 20:17

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

4 Comments

The fact that it's possible does not necessarily imply that it's a good practice, IMO.
You could always add comments of some kind to clarify that the holes are intentional: array=[ /* 0 */, /* 1 */, "cat", "dog" ]
...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.
Note that as the spec. denotes, the elements will be not defined, which is a bit different than being undefined, 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)
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.

1 Comment

array.unshift(undefined,undefined) would also work since you can pass multiple arguments, just like .push().
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]);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.