Modern browsers and environments like Node.js allow you to say {a:1, b:2,} or [1,2,3,]. This has historically been problematic with Internet Explorer. Is this fixed in Internet Explorer 9?
-
3Not sure, but is there any reason you would leave a trailing `,`? – alex Feb 18 '11 at 01:15
-
4@alex: By accident. Trust me. – SLaks Feb 18 '11 at 01:15
-
I know this bit me big time. Such a small thing to see... – Jared Farrish Feb 18 '11 at 03:55
-
15When hand editing code, leaving a trailing comma is very nice, and reduces potential future error if adding or removing lines. – Bryce May 18 '15 at 17:39
-
@alex another reason is if you want to construct a JS array from the back end `[ {{ for_each elem in list }} {{elem}}, {{end}}] `. Syntax is made up. – Alexander Suraphel Jul 03 '17 at 12:43
2 Answers
There are two different answers to this, one for dangling commas in object initializers and one for dangling commas in array initializers:
For object initializers, e.g.:
var obj = {
a: 1,
b: 2,
c: 3,
};
It's fixed in IE8 and above. Test it here: http://jsbin.com/UXuHopeC/1 (source). IE7 and earlier will throw a syntax error on the }
after the dangling comma.
For array initializers, e.g.:
var arr = [
1,
2,
3,
];
It was "fixed" in IE9 and above. Test it here: http://jsbin.com/UXuHopeC/2 (source). IE8 and earlier will give that array four entries, the last one having the value undefined
. IE9 and above give it three entries.
I put "fixed" in quotes because the spec was originally unclear about whether the array should have a final undefined
entry or not, so neither behavior was incorrect. It's just that IE went one way and everyone else went the other. :-)

- 1,031,962
- 187
- 1,923
- 1,875
-
Note that there are some versions of IE where this breaks... I just had an issue filed with our software in `IE 9 Update 9.0.30` because of a trailing comma in an object literal. Beating myself over the head for having chosen to leave that in there since we're no longer supporting IE 8 and below. – Ruan Mendes Jan 06 '15 at 14:12
-
@JuanMendes: I don't believe any version of IE9 has the trailing comma in object literal bug, as it was fixed in IE8. – T.J. Crowder Jan 06 '15 at 14:13
-
2The customer just filed the bug saying that it was fine in `IE 9 Update 9.0.23` but breaks in `IE 9 Update 9.0.30`, it's likely a regression that was fixed almost immediately. I'm trying to get my hands on that specific version to prove it, but the report looks like the customer did apply due diligence. – Ruan Mendes Jan 06 '15 at 14:16
-
-
What's the difference between an "initalizer" and a literal? If I say `var a = [1,];` is that different than `my_func([1,]);`? – Nick T Oct 28 '15 at 18:39
-
@NickT: There is none, "initializer" is just the word the specification uses for these. Most people call them "array literals" and "object literals." The spec doesn't, it only uses "literal" for things like string literals, numeric literals, and (interestingly) regular expression literals. If I had to guess, it would be that the distinction they're making is that a literal doesn't contain any expressions, but an initializer can. But I'm guessing there. – T.J. Crowder Oct 28 '15 at 18:48
-
@JuanMendes I found a table with these 9.0.x versions: [Internet Explorer 9#Releasegeschiedenis](https://nl.wikipedia.org/wiki/Internet_Explorer_9#Releasegeschiedenis) (yeah, it's in Dutch) – Gras Double Jun 25 '17 at 10:37
This document claims it is/will be corrected: http://blogs.msdn.com/b/ie/archive/2010/06/25/enhanced-scripting-in-ie9-ecmascript-5-support-and-more.aspx
Corrected Issues
Trailing commas in array literals added to the array’s length
Example
var len = [1,2,3,].length;
alert(len); //should be 3, IE8 says 4
It makes no specific mention of Objects. Just Arrays.
EDIT: More info. From this PDF document:
http://download.microsoft.com/download/8/4/2/8427CF1B-08B3-4557-952D-102E7A8FA64C/[MS-ES3].pdf
...dowloaded from this page: http://msdn.microsoft.com/en-us/library/ff520996(VS.85).aspx
JScript 5.8 supports the occurrence of a single trailing comma as the last item within an ObjectLiteral. JScript 5.7 does not support this extension.

- 1
- 1

- 318,772
- 63
- 451
- 440