Array bug?

Hi all,

If I fill an array in this way:

	for(var i=1;i<=7;i++)
		_myArray[i] = i;

And I check the “.length”,I obtain 8 but the elements are 7!

if,instead,I create the array with the code:

var _myArray = new Array(1,2,3,4,5,6,7)

Then the “.length” returns the right number(7).

Maybe something wrong with the array index that start from 1 and end with 7? Or I’m forgotten something?
Here a sample:
[attachment=0]test_array_length.servoy[/attachment]

You are right. The problem is the index. JavaScript Arrays always start at index 0.
E.g.:

var myArray = [1,2,3,4];  // myArray.length is 4, indexes range from 0 to 3.
myArray[8] = 8;          // myArray.length is now 9, myArray[4]..myArray[7] are undefined

Regards

Thanks for your answer Birgit! :)

So because this is a JavaScript’s behavior ,we have to take it as it is,right?

I’m using other count logic (a simple “for in”), I’m asking it just for knowledge

Array behaviour is different in almost every language. An interesting link showing this is:

Regards