ECMAScript Array Instantiation

There are two ways of instantiating Arrays in EMCAScript (if you exclude every conceivable hack that you could pull out of your hat just to show me that there are other ways as well). One1 uses the regular object instantiation syntax, namely new Array and it’s also possible to omit the new in order to call Array as a function instead (the latter is simply an alias of the former as can be seen in the specification). The other way is leaner on the fingers and is written as [], called the literal notation2.

Conventionally I always use the literal notation (except for in a single case—which I’ll get to later). The reason is that the Array constructor is overloaded into two distinct functions where one is a special case. Essentially (although in practice, not exactly) the special case function is called if the constructor has a single argument whose type is [Number]. The normal case creates an array containing all objects passed as arguments to the constructor. The special case creates an Array with its length matching the value of the argument passed to the constructor. This is where the interface breaks down.

Suppose I want to create an Array of two numbers (1 and 2) and I’m using the regular instantiation syntax, this is done by typing new Array (1, 2); (or, as described, Array (1, 2);). If on the other hand I want to create an array of only one number (3), what seems logical would be to type new Array(3);. Wrong. What just got instantiated was an array equal to one that can be created by the literal notation [undefined, undefined, undefined]. There is simply no way to create an Array of precisely one numerical element using this syntax.

If instead the literal notation was used per convention, the problem would never occur.

This is why I suggest that the following convention should be in place:

The literal notation for instantiating Arrays should always be used when specific values are to be inserted into the array upon creation. If an Array of a known amount of elements is to be created, the regular object instantiation syntax should be used, as in new Array (len). The code in place should make it clear that the argument len is numerical. Whether to use the new prefix or not for this case will depend on other conventions.

As for the use of the special case, I can imagine cases where using it would be solely over-optimization. If for instance an Array is to be instantly filled to a known size (where we don’t want to add the elements in the same statement as we instantiate the Array) one could use the special case to prevent unnecessary resizing of the Array (Array being of the ArrayList ADT). However it seems to me that one should empirically prove that this is a bottleneck before such an operation is used. On the other hand I might be overly attached to the literal notation. I have a hard time seeing why this operation would hurt.

A case where an Array won’t be filled but still needs a certain size would be bulletproof usage of the special case.


  1. ECMA-262, §§15.4.1-2, p. 88 

  2. ECMA-262, §11.1.4, p. 40 

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>. Beside the tag style "<foo>" it is also possible to use "[foo]".

More information about formatting options