admin管理员组

文章数量:1355996

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);

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);
Share Improve this question asked Nov 11, 2012 at 4:07 101V101V 5021 gold badge6 silver badges13 bronze badges 4
  • 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 Commented Nov 11, 2012 at 4:12
  • That clarifies it. Thanks jfriend00. – 101V Commented Nov 11, 2012 at 6: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 Commented Nov 11, 2012 at 16:21
  • 1 You should never need negative indices on an array. Have a look at this question for alternative strategies – Bergi Commented Nov 11, 2012 at 16:57
Add a ment  | 

3 Answers 3

Reset to default 6

I'll convert my original ment 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.

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

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

本文标签: Why array in JavaScript showing wrong lengthStack Overflow