1
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

Igor
  • 60,821
  • 10
  • 100
  • 175

4 Answers4

2

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.

bipll
  • 11,747
  • 1
  • 18
  • 32
2

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);
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
  • I asked a question about Sparse arrays, but I am still not satisfied, what is a "sparse" array ? – doubleOrt Oct 01 '17 at 18:43
  • that is an array which have holes with not assigned values. even `undefined` is a value, which may not distinuish from a sparse value which show up with `undefined` as well. the only reliable check is with [`in`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/in) operator. – Nina Scholz Oct 01 '17 at 18:52
  • So, the array is **not** filled with undefined values ? Does it only contain 1 element ? – doubleOrt Oct 01 '17 at 18:55
  • right, the undefined shows up with not given index, but also with given index and value of undefined. – Nina Scholz Oct 01 '17 at 18:57
  • Sure, but it seems weirdly inefficient to have an array that has only one element and then sort of "lie" about it when the `length` property is accessed on that array. – doubleOrt Oct 01 '17 at 19:00
  • you may have a look here, for example: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/keys#Key_iterator_doesn't_ignore_holes – Nina Scholz Oct 01 '17 at 19:01
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/155719/discussion-between-nina-scholz-and-taurus). – Nina Scholz Oct 01 '17 at 19:02
1

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.

doubleOrt
  • 2,407
  • 1
  • 14
  • 34
1

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.

madhuri
  • 11
  • 1
  • you can try it out after the programme runs Try and fetch any value between 0 to 999999 index you will get undefined. – madhuri Oct 01 '17 at 18:32
  • Actually, I don't think any `undefined` values are inserted, instead, you will get what is called a **sparse** array. – doubleOrt Oct 01 '17 at 18:43