Late last year, a discussion between myself and Steve Carlson (also on the My Yahoo! team) led to us discovering that array literals could be specified with empty values by putting commas in without values, such as:
var values = [,,,,,];
This creates an array whose values are all
undefined. The fun part is, once again, with browser differences. In Internet Explorer, this creates an array of six items. There are five commas so it assumes that you’re separated six values…makes sense, right? In all the other browsers (Firefox, Safari, Opera), it creates an array of five items. Why does this happen? This can be seen more clearly in the following example:
var values = [1,2,];
In Internet Explorer, this creates a three-item array where the values are 1, 2, and
undefined. In the other browsers, it creates a two-item array where the values are 1 and 2. So which of these two groups is correct? By default, one would assume that IE is wrong since it usually is.
ECMA-262 (p.41) indicates that a dangling comma is legal syntax. Commas in array initializers are represented by the Elision production. Without going into all of the technical mumbo jumbo, the specification basically states that each Elision adds 1 to the array’s
length property. This means that, as usual, IE’s implementation is incorrect since five commas should equal five items, not six.
It’s really frustrating to me that now, eight years after the third edition of ECMA-262 was finalized that browsers still don’t have this specification implemented perfectly.