IE Is Being Mean to Me: Episode 1 – Trailing Comma

This post is about a mistake that I made in my JavaScript code.

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.

  1. Trailing commas in JavaScript
  2. The Curious Case of Trailing Commas in IE
  3. My IE9 is fine with trailing comma, user’s IE9 is not; why?
  4. 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.

Advertisements

2 thoughts on “IE Is Being Mean to Me: Episode 1 – Trailing Comma

  1. Pingback: Razor Learning Notes | cuteprogramming
  2. Pingback: Nice Report Always Comes with Colourful Charts | cuteprogramming

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s