0

For some reason [,] is a valid array literal in javascript. It is a 1-element array. [,,] works as well, and so on. I am looking at the grammar to see why this parses, but as far as I can tell an empty expression shouldn't be allowed there.

Any ideas?

22
  • 6
    It's the same as asking why [] works. Commented Apr 19, 2014 at 21:09
  • 2
    Why wouldn't an empty expression work ? Seems like it should to me Commented Apr 19, 2014 at 21:09
  • My guess it that [,] === [undefined, undefined]. Commented Apr 19, 2014 at 21:10
  • 2
    @Johan - [,] would be the same as [undefined] since the last comma with empty item is ignored. Commented Apr 19, 2014 at 21:15
  • 1
    Which grammar are you looking at? You should be looking into the ECMAScript Language Specification, 5.1 Edition (HTML version) Commented Apr 19, 2014 at 21:18

2 Answers 2

2

It seems I was looking at an older language spec. The ECMAScript Language Specification, 5.1 Edition (HTML version)

Defines an ArrayLiteral as:

ArrayLiteral :
    [ Elision opt ]
    [ ElementList ]
    [ ElementList , Elisionopt ]

Elision :
    ,
    Elision ,

The Elision rule seems to define this case

It allows for array element "placeholders" in some sense.

I ran into this issue when dealing with leading commas:

var x = [
  , something
  , somethingElse
  , 2
] 

I was wondering why I was getting an undefined for the first element.

Thanks PointedEars and Derek 朕會功夫

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

3 Comments

Or in the official PDF specification, page 63, bottom.
You must be mistaken. The Array initialiser was introduced with ECMAScript Edition 3, and the productions ArrayLiteral : [ Elision_opt ] and Elision : , are on page 40 of the December 1999 revision.
To be clear, an elision doesn't create a member with a value of undefined, it just increases the length, there is no member. var a = ['a',,'b'] creates an array with length 3 and no member at position 1, i.e. '1' in a returns false. And IE has (had?) an issue with trailing elisions, it increases the length by one too many: [,].length should be 1, but IE says 2.
0

Wow, this is a very good question. I have never noticed it before.

I have made a fiddle that further 'exploits' this phenomena: http://jsfiddle.net/xJnrK/

I think it has something to do with the way javascript handles undefined variables. Javascript uses either 'null' or 'undefined' for empty variables. My guess it is just to prevent code to crash unlike Java where you must give all variables you call a value.

It gets more crazy, undefined is not a reserved word. You can actually override it (in a local scope) using:

function replicateMozilla()
{
    var undefined = '2';

    console.log(undefined);
}

replicateMozilla();

More about undefined: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/undefined

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.