4

I am learning Javascript. As a part of learning, I came across following scenario, where I expect the a1.length (the last line of the code) to show 201, but it shows 101, any Idea?

var a1 = new Array();

for (var i = -100; i<=100; i++)
 a1[i]  = i;

for (var i in a1)
{
    document.write(i + "=" + a1[i])
    document.write("<br>");
}

document.write(a1.length);
101V
  • 492
  • 1
  • 6
  • 13
  • 8
    Array indexes that are counted in `.length` go from 0 and up. Negative indexes are considered properties of the object, not array values. – jfriend00 Nov 11 '12 at 04:12
  • That clarifies it. Thanks jfriend00. – 101V Nov 11 '12 at 06:16
  • 3
    Using `for (var i in a1)` for iterate over an array is highly inappropriate. NEVER use that kind of loop to iterate over an array - always use `for(var i = 0; i < arr.length; i++)` and avoid things like negative indexes. An array, by definition, has elements from `0` up to `length-1` – ThiefMaster Nov 11 '12 at 16:21
  • 1
    You should never need negative indices on an array. Have a look at [this question](http://stackoverflow.com/q/10120126/1048572) for alternative strategies – Bergi Nov 11 '12 at 16:57

3 Answers3

6

I'll convert my original comment to a more thorough answer.

Array indexes that are counted in .length go from 0 and up. Negative indexes are considered properties of the object, not array values. As you can see from the ECMAScript spec below, array indexes are essentially just certain types of property values given some special treatment.

From section 15.4 of the ECMAScript spec:

15.4 Array Objects

Array objects give special treatment to a certain class of property names. A property name P (in the form of a String value) is an array index if and only if ToString(ToUint32(P)) is equal to P and ToUint32(P) is not equal to 2^32. A property whose property name is an array index is also called an element. Every Array object has a length property whose value is always a nonnegative integer less than 2^32 . The value of the length property is numerically greater than the name of every property whose name is an array index; whenever a property of an Array object is created or changed, other properties are adjusted as necessary to maintain this invariant. Specifically, whenever a property is added whose name is an array index, the length property is changed, if necessary, to be one more than the numeric value of that array index; and whenever the length property is changed, every property whose name is an array index whose value is not smaller than the new length is automatically deleted. This constraint applies only to own properties of an Array object and is unaffected by length or array index properties that may be inherited from its prototypes.


Also, you should never "iterate" arrays with a for-in-loop:

for (var i in a1)

That iterates all enumerable properties of a1 which will include all array indexes, but could also include other properties. If you want to iterate only array elements with a for loop, you should use the other form:

for (var i = 0, len = a1.length; i < len; i++) 

It is slightly more typing, but a lot safer.

Or, in more modern browsers, you can use the .forEach() method.

Community
  • 1
  • 1
jfriend00
  • 683,504
  • 96
  • 985
  • 979
1

It is because arrays in Javascript are zero-based, i.e. they start from zero and go up to length - 1.

You usually write your for loops to be bound by less-than operator like this:

for(i = 0; i < arr.length; i++) {
    // do something with arr[i]
}
Community
  • 1
  • 1
Spoike
  • 119,724
  • 44
  • 140
  • 158
0

The array length is defined as the index of the last element plus one. Arrays do not need to be continuous, which can give strange results:

var myArray = [];
myArray[-42] = 1
myArray[1000] = 2;
document.write(myArray.length); // 1001
Thijs van Dien
  • 6,516
  • 1
  • 29
  • 48