4

Can anyone explain why the second alert says 0 ?

  var pollData = new Array();
  pollData['pollType'] = 2;
  alert(pollData['pollType']); // This prints 2
  alert(pollData.length); // This prints 0 ??
1
  • that shit cray. don't do that to arrays u make my brain hurt Commented May 27, 2014 at 15:37

6 Answers 6

8

The length of the array is only changed when you add numeric indexes. For example,

pollData["randomString"] = 23;

has no effect on length, but

var pollData = [];
pollData["45"] = "Hello";
pollData.length; // 46

changes the length to 46. Note that it doesn't matter if the key was a number or a string, as long as it is a numeric integer.

Besides, you are not supposed to use arrays in this manner. Consider it more of a side effect, since arrays are objects too, and in JavaScript any object can hold arbitrary keys as strings.

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

5 Comments

Can I have the key as the string 'pollType'?
Yes, you can. The example in your question does exactly that. But it's not a good practice. Do you want to store additional data related to the array somewhere?
Yeah but that doesn't add them to the array as the length is still 0
Thank you for this! Made a fiddle, because nobody believed me that it worked this way: jsfiddle.net/6WrkT/1
I know this is late but wanted to add that creating an array using a constructor also cause this issue. var pollData = new Array(45);
2

Because you haven't put anything into the array yet. You've only been assigning to a dynamically-created pollType attribute on the array object.

If you use numeric indices, then the array automagically takes care of length. For example:

var arr = [  ];    // same as new Array()
arr[2] = 'Banana!';
alert(arr.length);    // prints 3 (indexes 0 through 2 were created)

Comments

0

The length property takes into consideration only those members of the array which names are indexes (like '1', '2', '3', ... ).

Comments

0

Arrays in JavaScript have numeric indexes only.

Use an object, which is essentially what you are doing above, setting properties on that array object.

Comments

0

array.length returns how many values are stored in the array. The first alert is returning the value of the position 'pollType'.

The reference guide I always use when needing help with javascript arrays is this page http://www.hunlock.com/blogs/Mastering_Javascript_Arrays

I'd also read what it says under the heading Javascript Does Not Support Associative Arrays, as you may run into problems with this also.

1 Comment

Just want to note, that while that's what Array.length supposedly does, it doesn't behave that way. Here: jsfiddle.net/6WrkT/1
0

var pollData = Array();

function test() {
    pollData[0] = 2
    alert(pollData[0]);
    alert(pollData.length);
}

//[x] is the array position; hence ['polltype'] is causing issues

Comments

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.