32

I am wondering if a comma trailing an array in javascript is valid?

var settings = {
    'foo'  : oof,
    'bar' : rab,
};

vs

var settings = {
    'foo'  : oof,
    'bar' : rab
};

Note the second example does not have a comma after the last key/value.

superUntitled
  • 22,351
  • 30
  • 83
  • 110
  • Suggest (1) title change to "...in an object or array" and (2) additional example with a trailing comma in an array literal. `var array_literal = ['foo', 'bar',];` – Bob Stein Dec 31 '15 at 12:09
  • 1
    This question provides info about IE support: [Does Internet Explorer 9 choke on extra commas at the end of array and object literals?](https://stackoverflow.com/questions/5036618/does-internet-explorer-9-choke-on-extra-commas-at-the-end-of-array-and-object-li) – In a nutshell, if you support only IE9+, you can use trailing commas. – Gras Double Jun 25 '17 at 10:33

3 Answers3

55

Most browsers and implementations do allow a trailing comma, the big BUT is the "Internet Explorer".

A trailing comma in most InternetExplorer versions causes BIG trouble. It'll throw wierd, crazy, strange and unreasonable errors .. you have no idea where you're at! This is terrible, you'll fall into deep and serious depressions. The disease also has a name, "evil comma of doom" it was called once.

Conclusion: NEVER.. ever! use a trailing comma in Javascript.

jAndy
  • 231,737
  • 57
  • 305
  • 359
  • Thanks jAndy, i now know of the 'ecod'. is it semantically incorrect, or just a bug in ie? – superUntitled Feb 28 '11 at 07:46
  • 1
    It’s rather the Internet Explorer that is the *eval browser of doom*. ;) – Gumbo Feb 28 '11 at 07:56
  • @Gumbo, @superUntitle: Like Anurag mentioned, in this case it was actually a pretty correct implementation of the ES specs (however, even if the error messages are completely useless). It looks more like they did it accidentally :-) – jAndy Feb 28 '11 at 07:59
  • 6
    I know its late, but in case anybody spots this. More modern version of IE (IE9/10) will cater for this perfectly. < IE8 however, and expect pain. – KingCronus May 07 '13 at 09:31
  • If developing for NodeJS (which uses v8) I prefer always having the trailing commas as it looks better when adding/removing properties/methods in your VCS's diff :-) If looking for a compatibility table: http://rauschma.github.io/js-feature-matrix/ – Thomas Jensen Feb 24 '14 at 16:02
  • [This resource](http://rauschma.github.io/js-feature-matrix/) says IE8,7,6 do not support trailing commas in **object literals**. No mention of array literals. – Bob Stein Dec 31 '15 at 12:11
  • IE8 may give "Object expected" error in such cases. – Andrei May 30 '16 at 13:04
31

Historically speaking, ES3 did NOT allow a trailing comma when defining an object literal. This was one thing that IE did get right, but most other bowser vendors went against the spec and allowed it anyways. So technically it was a bug in the other browsers that supported it. In ES3, an ObjectLiteral was defined as,

ObjectLiteral
    { }
    { PropertyNameAndValueList }

Later ES5 resolved this issue by going with the majority and legitimizing the trailing comma by putting it in the spec. Now an ObjectLiteral is defined as,

ObjectLiteral
    { }
    { PropertyNameAndValueList } 
    { PropertyNameAndValueList , }

Notice the trailing comma at the end.

Although the trailing comma is defined in an object literal, it is still not allowed in JSON according to ES5. So while the following object literal is valid,

{ foo: "bar", }

the following JSON is not,

'{ "foo": "bar", }'

The grammar for a JSONObject is,

JSONObject
    { }
    { JSONMemberList }

JSONMemberList
    JSONMember  
    JSONMemberList , JSONMember

JSONMember
    JSONString : JSONValue 

In short, if you don't want to worry about spec or browser quirks, then do NOT add a trailing comma.

Anurag
  • 140,337
  • 36
  • 221
  • 257
1

With regards to object literals at least (the question's title mentions array, but the example is an object literal) it depends on where your js is running. Trailing commas in object literals are valid in ES5. Although trailing commas can cause problems on the web (in IE) most minifiers will remove them anyway.

http://www.2ality.com/2011/06/object-literal-comma.html

http://dontkry.com/posts/code/trailing-commas.html

Peter O'Brien
  • 185
  • 1
  • 7