Wednesday, July 14, 2004

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:

At 4:00 AM, Blogger Ayette said...

IE is correct

 
At 7:36 AM, Blogger Richard Baker said...

Fantastically helpful! Thank you!!!

 
At 4:31 AM, Blogger loky said...

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 !

 
At 3:20 PM, Blogger slaists said...

Before get array length check what you use IE or Fox!

 
At 4:24 AM, Blogger Unknown said...

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.

 
At 3:55 AM, Blogger gonzales said...

"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?

 
At 1:02 PM, Blogger Luis Maurix said...

The only way to find this is via CGI usage, so you can crop that extra comma with a substrstr = (str,ini,end)

 
At 8:18 AM, Blogger Simon Howard said...

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.

 
At 1:08 AM, Blogger Unknown said...

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.

 
At 9:46 PM, Blogger c4r5 said...

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

 
At 4:22 PM, Blogger m said...

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.

 
At 5:31 PM, Blogger Chris Marx said...

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...
}

 
At 9:06 AM, Anonymous Anonymous said...

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)

 
At 12:19 AM, Blogger Unknown said...

c is right. The standard means "before" not "after".
The other js engines from safari and opera behave like FF SpiderMonkey

 
At 6:49 AM, Blogger shyam said...

great thanks man....

 
At 11:29 AM, Blogger Unknown said...

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

 
At 3:59 AM, Anonymous Anonymous said...

Thank you for this, was a complete headache trying to track this down!

 
At 10:07 AM, Anonymous Anonymous said...

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.

 
At 10:12 AM, Blogger Lenny said...

AAarrrrghhh.... whole morning wasted on this. Thank you for posting this info!!!

 

Post a Comment

<< Home