myArray = [];
myArray.length;
myArray[1000000] = true;
myArray.length;
why does this return 100001? ive read this in douglas crockford book and i dont understand what this means
myArray = [];
myArray.length;
myArray[1000000] = true;
myArray.length;
why does this return 100001? ive read this in douglas crockford book and i dont understand what this means
1000001, you've probably meant. An array is extended as soon as you access its element beyond current bounds. That is why after you touch its element no. 10⁶, it becomes of 10⁶ + 1 elements.
Maybe you check with a smaller index, like 10 and have a look to the values of the array.
The result is a sparse array with missing indices and one value on index 10.
The length is, as always, last index plus one.
var myArray = [];
console.log(myArray.length);
myArray[10] = true;
console.log(myArray.length);
console.log(myArray);
Arrays start at index 0, so if we have an array:
var arr = [1,2,3,4,5];
arr[0]
is 1
, arr[1]
is 2
and so on, and the array's length is 5
, because it contains 5 "elements". So, arrays start at index 0 (if you wanted to get the first element of the array, you would have to run arr[0]
, not arr[1]
, because arr[1]
would be the second element) but .length
counts the elements and starts with "1" for the first element, "2" for the second element, etc.
Now when you run this:
var arr = [1,2,3,4,5];
arr[200] = "whatever";
console.log(arr);
You will get this output:
(201) [1, 2, 3, 4, 5, undefined × 195, "whatever"]
So when we randomly added an element to the array at index 200
(which is actually the 201th element in the array, since arrays start at 0, not 1), the array's length just grew from 5
to 201
(the same thing can be seen in the output), and as you can see in the output, the array now contains the original 5 elements (1
, 2
, 3
, 4
and 5
), 195 ghost elements whose value undefined
when you query them (don't bother what I mean by ghost element just now though), and 1 "whatever"
element at its end (the output in the console would be slightly different if you manually inserted the 195 undefined
elements, but don't bother yourself with such things for now).
So in your case, an array is defined (myArray
), by default, it contains nothing, so it has a length of 0
. Then you go on and say "I want to add an element at the 1000000th index of this array" (the line myArray[1000000] = true
), don't let the true
value of the added element confuse you! the output would be the same regardless of what followed the myArray[1000000] =
part, so myArray[1000000] = "whatever"
would give you the same output: an array whose length is 1000001
. Here is the output of your array when used with console.log(..)
(remember, the output would be the same even if "whatever"
was inserted in place of true
):
(1000001) [undefined × 1000000, true]
Also, remember, the array you get when you say var arr = []; arr[1000] = "foo"
is sparse (If you are interested in sparse arrays)
The other comments have answered your question correctly as well but I decided to do so because: 1. I have a bad feeling the true
part in myArray[1000000] = true
is confusing you. 2. They explained the arrays-start-at-0 thing in a not-too-clear way.
It returns 100001 because, myArray = []; creates an empty array
That's why myArray.length; = 0
When you do myArray[1000000] = true;
that time actually what happens is it puts all values from 0th index to 999999th index to undefined and then puts you 1000000 value to true. so actually there are 1000001 values in an array. That's why the length is 1000001
every function of array specifies that array works as a collection of things in a sequence which means you cant put value at random place even if you provide any random index the by the default value (undefined) will be available as a value for other places.