Array length headaches
A co-worker of mine stumbled across this little gem:
var foo = ['a', 'b', 'c', 'd',]; document.write(foo.length);
Moz/Firefox will print 4. IE6 will print 5. Why?
Look again at the array. See the extra comma on the right? IE6 interprets this as an additional element with an undefined value. Moz/Firefox ignores it.
19 Comments:
IE is correct
Fantastically helpful! Thank you!!!
Hey, this issue is paining me. I have an array that WILL have an extra comma at the end. & then I have to iterate over that array.
how do I make that work, coz the length variable gives different number for IE & fox !
Before get array length check what you use IE or Fox!
I can't believe the disrespect for Firefox, it's support of standards is far Far FAR greater than Internet Explorer.
"I have an array that WILL have an extra comma at the end."
That's incorrect, why WILL your code have an incorrect comma and why do expect it to work properly.
"would be nice if FF would stop pulling crap like this."
Why you would expect FF to count something that is NOT there baffles me....or do you just code for IE and expect FF to behave the same way?
The only way to find this is via CGI usage, so you can crop that extra comma with a substrstr = (str,ini,end)
IE is correct, Mozilla is wrong.
From the ECMAScript standard:
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 AssignmentExpression (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.
actually, that ECMA script description is, upon my inspection, defining the case that an element will be made for blank space occurring BEFOE a comma, but not AFTER. Therefore, the un-terminated "entry" after the last comma does not force another array entry, thus IE is, once again, incorrect in the interpretation of a standard.
IE is correct...
var foo = [, , , ,];
surely one would expect undefined values to be before, between, AND AFTER comments.
neither IE and FF are perfect, IE is far superior when it comes to Intranet applications
you never want to count an extra element.
the reason you want the freedom to put the trailing comma without consequences is so that writing code like this is safe:
var a = {
foo : bar,
abc : xyz,
last : one,
};
(this blog does not allow PRE tags!)
now you can safely add another line (with cut-paste/kill-yank) without having to manage commas. perl folks should be vehemently nodding their heads right now.
i did this
var ie = (navigator.userAgent.indexOf("MSIE")==-1)?false:true;
for (var i = 0;(G.ie)? i < someArray.length-1 : i < someArray.length; i++) {
//etc...
}
If you need an undefined element in your array, could you not use null? As opposed to foo = [ , , , , ] just use foo = [null, null, null, null, null]. Or more importantly, why bother with all those commas anyway when you can create a new array with undefined elements just by calling foo = new Array(5)
c is right. The standard means "before" not "after".
The other js engines from safari and opera behave like FF SpiderMonkey
great thanks man....
It seems to me that both js interpreters (IE & FF) should report a syntax error.
This still happens in IE8 and FF 3.6.3
Thank you for this, was a complete headache trying to track this down!
Why would anyone want to add a trailing comma to their array? Because it's so much easier to generate such strings in a foreach-style loop on the server side:
foreach (elem,list) printf("%s,",elem);
To NOT put a trailing comma in, you have to know whether the element you're appending to a string that's supposed to evaluate to a JavaScript array is the last one or not. This precludes the use of foreach-type loops altogether, requiring instead an indexed loop, and an explicit test of the index against the length of the array.
Thanks to Microsoft for keeping 1970s-style programming alive. I can't wait until they find a way to make assembly language necessary again.
AAarrrrghhh.... whole morning wasted on this. Thank you for posting this info!!!
Post a Comment
<< Home