<p>Code:</p>

<pre><code>&lt;html xmlns="http://www.w3.org/1999/xhtml"&gt;
&lt;head&gt;
    &lt;title&gt;Unusual Array Lengths!&lt;/title&gt;

    &lt;script type="text/javascript"&gt;
        var arrayList = new Array();
        arrayList = [1, 2, 3, 4, 5, ];
        alert(arrayList.length);
    &lt;/script&gt;

&lt;/head&gt;
&lt;body&gt;
&lt;/body&gt;
&lt;/html&gt;
</code></pre>

<p>Notice the extra comma in the array declaration.
The code above gives different outputs for various browsers:</p>

<p>Safari: 5</p>

<p>Firefox: 5</p>

<p>IE: 6</p>

<p>The extra comma in the array is being ignored by Safari and FF while IE treats it as another object in the array.</p>

<p>On some search, I have found mixed opinions about which answer is correct. Most people say that IE is correct but then Safari is also doing the same thing as Firefox. I haven't tested this on other browsers like Opera but I assume that there are discrepancies.</p>

<p>My questions:</p>

<p>i. <strong>Which one of these is correct?</strong></p>

<p><em>Edit: By general consensus (and ECMAScript guidelines) we assume that IE is again at fault.</em>  </p>

<p>ii. <strong>Are there any other such Javascript browser quirks that I should be wary of?</strong></p>

<p><em>Edit: Yes, there are loads of Javascript quirks. <a href="http://www.quirksmode.org" rel="nofollow noreferrer">www.quirksmode.org</a> is a good resource for the same.</em></p>

<p>iii. <strong>How do I avoid errors such as these?</strong></p>

<p><em>Edit: Use <a href="http://www.jslint.com/" rel="nofollow noreferrer">JSLint</a> to validate your javascript. Or, use some external <a href="http://openjsan.org/" rel="nofollow noreferrer">libraries</a>. Or, <a href="https://stackoverflow.com/questions/29053/javascript-browser-quirks-arraylength#29062">sanitize</a> your code.</em></p>

<p><em>Thanks to <a href="https://stackoverflow.com/users/3069/damien-b">DamienB</a>, <a href="https://stackoverflow.com/users/1790/jasonbunting">JasonBunting</a>, <a href="https://stackoverflow.com/users/2168/john">John</a> and <a href="https://stackoverflow.com/users/1968/konrad-rudolph">Konrad Rudolph</a> for their inputs.</em></p>

[{'answer_id': 29062, 'author': 'Damien B', 'author_id': 3069, 'author_profile': 'https://Stackoverflow.com/users/3069', 'pm_score': 2, 'selected': False, 'text': '<p>"3" for those cases, I usually put in my scripts </p>\n\n<pre><code>if(!arrayList[arrayList.length -1]) arrayList.pop();\n</code></pre>\n\n<p>You could make a utility function out of that.</p>\n'}, {'answer_id': 29066, 'author': 'Konrad Rudolph', 'author_id': 1968, 'author_profile': 'https://Stackoverflow.com/users/1968', 'pm_score': 3, 'selected': False, 'text': '<p>I was intrigued so I looked it up in the definition of <a href="http://www.ecma-international.org/publications/standards/Ecma-262.htm" rel="noreferrer">ECMAScript 262 ed. 3</a> which is the basis of JavaScript 1.8. The relevant definition is found in section 11.1.4 and unfortunately is not very clear. The section explicitly states that elisions (= omissions) at the beginning or in the middle don\'t define an element <em>but do contribute to the overall length</em>.</p>\n\n<p>There is no explicit statements about redundant commas at the end of the initializer but by omission I conclude that the above statement implies that they do <em>not</em> contribute to the overall length so I conclude that <strong>MSIE is wrong</strong>.</p>\n\n<p>The relevant paragraph reads as follows:</p>\n\n<blockquote>\n  <p>Array elements may be elided at the beginning, middle or end of the element list. Whenever a comma in the element list is not preceded by an Assignment Expression (i.e., a comma at the beginning or after another comma), the missing array element contributes to the length of the Array and increases the index of subsequent elements. Elided array elements are not defined. </p>\n</blockquote>\n'}, {'answer_id': 29073, 'author': 'John', 'author_id': 2168, 'author_profile': 'https://Stackoverflow.com/users/2168', 'pm_score': 4, 'selected': True, 'text': '<p>It seems to me that the Firefox behavior is correct.  What is the value of the 6th value in IE (sorry I don\'t have it handy to test).  Since there is no actual value provided, I imagine it\'s filling it with something like \'null\' which certainly doesn\'t seem to be what you intended to have happen when you created the array.</p>\n\n<p>At the end of the day though, it doesn\'t really matter which is "correct" since the reality is that either you are targeting only one browser, in which case you can ignore what the others do, or you are targeting multiple browsers in which case your code needs to work on all of them.  In this case the obvious solution is to never include the dangling comma in an array initializer.</p>\n\n<p>If you have problems avoiding it (e.g. for some reason you have developed a (bad, imho) habit of including it) and other problems like this, then something like <a href="http://www.jslint.com/" rel="noreferrer">JSLint</a> might help.  </p>\n'}, {'answer_id': 29082, 'author': 'Adhip Gupta', 'author_id': 384, 'author_profile': 'https://Stackoverflow.com/users/384', 'pm_score': 0, 'selected': False, 'text': '<p><a href="https://stackoverflow.com/questions/29053/javascript-browser-quirks-arraylength#29073">@John</a>: The value of arrayList[5] comes out to be \'undefined\'.</p>\n\n<p>Yes, there should never be a dangling comma in declarations. Actually, I was just going through someone else\'s long long javascript code which somehow was not working correctly in different browers. Turned out that the dangling comma was the culprit that has accidently been typed in! :)</p>\n'}, {'answer_id': 29134, 'author': 'Jason Bunting', 'author_id': 1790, 'author_profile': 'https://Stackoverflow.com/users/1790', 'pm_score': 2, 'selected': False, 'text': '<p>First off, <strong><a href="https://stackoverflow.com/questions/29053/javascript-browser-quirks-arraylength#29066">Konrad</a></strong> is right to quote the spec, as that is what defines the language and answers your first question.</p>\n\n<p>To answer your other questions:</p>\n\n<blockquote>\n  <p><em>Are there any other such Javascript browser quirks that I should be wary of?</em></p>\n</blockquote>\n\n<p>Oh, too many to list here! Try <strong><a href="http://www.quirksmode.org/" rel="nofollow noreferrer">the QuirksMode website</a></strong> for a good place to find nearly everything known.</p>\n\n<blockquote>\n  <p><em>How do I avoid errors such as these?</em></p>\n</blockquote>\n\n<p>The best way is to use <a href="http://openjsan.org/" rel="nofollow noreferrer"><strong>a library</strong></a> that abstracts these problems away for you so that you can get down to worrying about the logic of the application. Although a bit esoteric, I prefer and recommend <a href="http://openjsan.org/" rel="nofollow noreferrer"><strong>MochiKit</strong></a>.</p>\n'}, {'answer_id': 29173, 'author': 'Shadow2531', 'author_id': 1697, 'author_profile': 'https://Stackoverflow.com/users/1697', 'pm_score': 1, 'selected': False, 'text': '<blockquote>\n  <p>Which one of these is correct?</p>\n</blockquote>\n\n<p>Opera also returns 5. That means IE is outnumbered and majority rules as far as what you should expect.</p>\n'}, {'answer_id': 9964192, 'author': 'Austin France', 'author_id': 644634, 'author_profile': 'https://Stackoverflow.com/users/644634', 'pm_score': 1, 'selected': False, 'text': '<p>Ecma 262 edition 5.1 section 11.1.4 array initializer states that a comma at the end if the array does not contribute to the length if the array. "if an element is elided at the end of the array it does not contribute to the length of the array"</p>\n\n<p>That means [ "x", ] is perfectly legal javascript and should return an array of length 1</p>\n'}]
['https://Stackoverflow.com/questions/29053', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/384/']