Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript Browser Quirks - array.Length

Tags:

javascript

Code:

<html xmlns="http://www.w3.org/1999/xhtml"> <head>     <title>Unusual Array Lengths!</title>      <script type="text/javascript">         var arrayList = new Array();         arrayList = [1, 2, 3, 4, 5, ];         alert(arrayList.length);     </script>  </head> <body> </body> </html> 

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

Safari: 5

Firefox: 5

IE: 6

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

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.

My questions:

i. Which one of these is correct?

Edit: By general consensus (and ECMAScript guidelines) we assume that IE is again at fault.

ii. Are there any other such Javascript browser quirks that I should be wary of?

Edit: Yes, there are loads of Javascript quirks. www.quirksmode.org is a good resource for the same.

iii. How do I avoid errors such as these?

Edit: Use JSLint to validate your javascript. Or, use some external libraries. Or, sanitize your code.

Thanks to DamienB, JasonBunting, John and Konrad Rudolph for their inputs.

like image 558
Adhip Gupta Avatar asked Aug 26 '08 22:08

Adhip Gupta


People also ask

How long can an array in JavaScript?

What exactly is the JavaScript Array length property. By definition, the length property of an array is an unsigned, 32-bit integer that is always numerically greater than the highest index in the array. The value of the length is 232. It means that an array can hold up to 4294967296 (232) elements.

What does array length 1 mean in JavaScript?

length -1 means, specifically the -1 part. When using a for loop over an array we have something like this: for (i = 0; i < array.

Can JavaScript array length 0?

2) Setting its length to zerolength = 0; The length property is read/write property of an Array object. When the length property is set to zero, all elements of the array are automatically deleted.

How do you find the length of an element in an array?

The simplest procedural way to get the value of the length of an array is by using the sizeof operator. First you need to determine the size of the array. Then you need to divide it by the size of one element.


2 Answers

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.

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.

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 JSLint might help.

like image 56
John Avatar answered Oct 07 '22 22:10

John


I was intrigued so I looked it up in the definition of ECMAScript 262 ed. 3 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 but do contribute to the overall length.

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 not contribute to the overall length so I conclude that MSIE is wrong.

The relevant paragraph reads as follows:

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.

like image 31
Konrad Rudolph Avatar answered Oct 07 '22 20:10

Konrad Rudolph