Last Comma in Object/Array Issue in IE

One super weird issue in IE is the way it handles the final comma in an array/object initialization - it simply ignores all the code from there. This bit of code will illustrate what I am trying to say...

var sample = {
	'hello':'world',
	'foo':'bar', //This is 'the last comma'
}
alert("Hello World");

In most browsers, the final ',' is optional - its presence will not make any difference in the script. But IE will decide to do something that defies explanation - it just ignores everything after that bit of code. The 'Hello World' alert will never be seen in an IE browser.

Live Evaluation

If you are lucky IE will throw an error when this happens - but I have come across situations when IE just silently ignores everything after such an event.

Fortunately, this problem can be easily avoided. Since the last ',' is optional, make sure that it is not present in the object initialization code.

blog comments powered by Disqus