3

If I have an array in JavaScript, say:

var a = [1, 2, 3];
var b = ["Praveen", "Kumar", "Stack", "Overflow"];

If I get the lengths of the above arrays by:

a.length;
b.length;

I get the correct values. i.e.,

a.length;  // 3
b.length;  // 4

But, if I create another array, where I set my indices like:

c = [];
c[5] = "Five";
c[10] = "Ten";

And then if I query the length, it shows me 11.

c.length  // 11

Is this wrong? Or is this way JavaScript interprets arrays? Please guide.

Praveen Kumar Purushothaman
  • 164,888
  • 24
  • 203
  • 252

3 Answers3

6

The .length is defined to be one greater than the value of the largest numeric index. (It's not just "numeric"; it's 32-bit integer values, but basically numbered properties.)

Conversely, setting the .length property to some numeric value (say, 6 in your example) has the effect of deleting properties whose property name is a number greater than or equal to the value you set it to.

Pointy
  • 405,095
  • 59
  • 585
  • 614
  • I don't know if this is gonna be the answer, but yea, a lot of people have voted for you, including me. So I will accept this! `:D` – Praveen Kumar Purushothaman Mar 27 '15 at 14:56
  • @NoDownvotesPlz But check out any of the programming languages, let it be Java, PHP or whatsoever. It shows the number of items in the array, contrary to the `lastIndex + 1`, right? That should be the way, if I am correct? – Praveen Kumar Purushothaman Mar 27 '15 at 14:57
  • `c[10]` simply means, there are 0 to 10 elements, i.e 11 elements in the array, thats in all languages – Naeem Shaikh Mar 27 '15 at 14:57
  • @PraveenKumar Array index starts from 0, so `c[10]` becomes 11th element.. simple count – Naeem Shaikh Mar 27 '15 at 14:58
  • But what if that's the only item? Count = Number of items in the array, not `last_insert_id`!!! Agree? – Praveen Kumar Purushothaman Mar 27 '15 at 15:03
  • 1
    @PraveenKumar: JavaScript arrays aren't wrong, the array implementation is just different from most languages. Languages with dynamic arrays often don't let you assign an item just anywhere in an array, you need to make sure that the array is large enough first. Resizing an array usually will create all the items to fill the array, while Javascript arrays only contain the actual items that you put in it. – Guffa Mar 27 '15 at 15:47
3

The effect of

var c = [];
c[10] = "foo";

is that c will look like this:

c[0] === undefined
c[1] === undefined
c[2] === undefined
c[3] === undefined
c[4] === undefined
c[5] === undefined
c[6] === undefined
c[7] === undefined
c[8] === undefined
c[9] === undefined
c[10] === "foo"

Having elements 0 through 10, the length of the array is therefore 11.

dgvid
  • 26,293
  • 5
  • 40
  • 57
  • 1
    JavaScript is often wrong. See the excellent book "JavaScript: The Good Parts" by Douglas Crockford for an extensive list. – dgvid Apr 01 '15 at 14:29
2

It's correct. The length depends on the highest index used, not the number of actual items in the array.

Creating an array like this:

c = [];
c[5] = "Five";
c[10] = "Ten";

Has the exact same effect as this:

c = [,,,,,"Five",,,,,"Ten"];

Although the array only has two items, the length is 11 as the highest index used is 10.

The string representation of the array will be [undefined, undefined, undefined, undefined, undefined, "Five", undefined, undefined, undefined, undefined, "Ten"] as it uses the length and shows a value for all indexes up to that.

If you loop the properties in the array, you will only get indexes of the actual items:

for (n in c) console.log(n);

Output:

5
10
Guffa
  • 687,336
  • 108
  • 737
  • 1,005