I always thought trailing commas were standard in all browsers. I tested my code on IE 10, Firefox and Chrome and it just worked fine. However, my code is just not working at all when tested it on IE 7 and IE 8. It turns out that IE 7 & 8 have not implemented ECMAScript 5 correctly because, in fact, if an element is elided at the end of an array, that element does not contribute to the length of the Array.
So if I run the following code in different browsers, I will get different results.
var test = [,,,,]; alert("Length of the array: " + test.length);
In IE 10, Firefox and Chrome, the length of the array will be 4. However, in IE 7 and 8, the length of the array is 5, which in this case does not chop off the “undefined” item at the end of the array.
Here is a list of online discussions that I found when I was trying to fix the bug.
- The Curious Case of Trailing Commas in IE
- My IE9 is fine with trailing comma, user’s IE9 is not; why?
- Jquery autoselect not working on IE8
Yup, so this post is just to remind me to be careful and not to repeat the same mistake again.
Finally, a song “IE Is Being Mean to Me” by Scott Ward that I found on YouTube.