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.

5
  • 3
    c[10] is add this value at position 10. Arrays run from 0 to their length, which means that that array has a length of 11. Commented Mar 26, 2015 at 14:00
  • 2
    if you think, length should be two then this is the answer , else what pointy said below Commented Mar 26, 2015 at 14:01
  • 1
    If you're looking to get the number of set items in the array, you can somewhat abuse Object.keys: Object.keys(c).length will return 2. Commented Mar 26, 2015 at 14:15
  • @NoDownvotesPlz Thanks for the pointer. ps: Nice username. Commented Mar 27, 2015 at 14:55
  • @ScottKaye Lemme check out. :D Commented Mar 27, 2015 at 14:56

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

6 Comments

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
@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?
c[10] simply means, there are 0 to 10 elements, i.e 11 elements in the array, thats in all languages
@PraveenKumar Array index starts from 0, so c[10] becomes 11th element.. simple count
@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.
|
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.

1 Comment

JavaScript is often wrong. See the excellent book "JavaScript: The Good Parts" by Douglas Crockford for an extensive list.
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

1 Comment

Yes, I saw it! :D In Chrome Console, it showed me undefined x 4 and undefined x 5!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.