101V
101V

Reputation: 502

Why array in JavaScript showing wrong 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);

Upvotes: 4

Views: 6241

Answers (3)

jfriend00
jfriend00

Reputation: 707308

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.

Upvotes: 6

Spoike
Spoike

Reputation: 121772

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

Upvotes: 1

Thijs van Dien
Thijs van Dien

Reputation: 6616

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

Upvotes: 0

Related Questions