Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
169
rated 0 times [  172] [ 3]  / answers: 1 / hits: 23739  / 9 Years ago, sun, july 5, 2015, 12:00:00

I just want to understand how Javascript arrays work but I have a complicated problem here.



First I created my array:



var arr = [];


And set some elements in it:



arr[5] = a thing;
arr[2] = undefined;


I thought that I should have an array of size 2, because I only have two objects at 2 specific indexes. So I tested it with the .length property of arrays:



document.write(arr.length + <br>);


The result, interestingly, is 6. But it must contain two items. How can its size be 6? It is probably related with the latest index that I used, here arr[5] = a thing;



I then tried to loop over it:



var size = 0;
for(var x in arr){
size++;
}


And the size variable is now 2. So, what I learned from this: if I use a for in loop, I will calculate how many properties are in it, not its last index.



But if I try to document.write(arr[4]) (which is not set yet), it writes undefined.



So why is arr[2] counted in the for..in loop, but not arr[4]?



Let me answer my question: what I was thinking about typeof undefined == undefined which is amazingly true. But this is JavaScript, we need to play with it using his own rules :)



jsFiddle and snippet below.





var arr = [];

arr[5] = a thing;
arr[2] = undefined;
document.write(arr.length + <br>);

var size = 0;
for(var x in arr){
size++;
}

document.write(size + <br>);

document.write(arr[4] + <br>);




More From » arrays

 Answers
27

Note: Array indexes are nothing but properties of Array objects.



Quoting MDN's Relationship between length and numerical properties section,




When setting a property on a JavaScript array when the property is a valid array index and that index is outside the current bounds of the array, the engine will update the array's length property accordingly.




Quoting ECMA Script 5 Specification of Array Objects,




whenever a property is added whose name is an array index, the length property is changed, if necessary, to be one more than the numeric value of that array index; and whenever the length property is changed, every property whose name is an array index whose value is not smaller than the new length is automatically deleted




So, when you set a value at index 5, JavaScript engine adjusts the length of the Array to 6.






Quoting ECMA Script 5 Specification of Array Objects,




A property name P (in the form of a String value) is an array index if and only if ToString(ToUint32(P)) is equal to P and ToUint32(P) is not equal to 232−1.




So, in your case 2 and 4 are valid indexes but only 2 is defined in the array. You can confirm that like this



arr.hasOwnProperty(2)


The other indexes are not defined in the array yet. So, your array object is called a sparse array object.




So why arr[2] is counted in for..in loop and not arr[4] is not counted?




The for..in enumerates all the valid enumerable properties of the object. In your case, since only 2 is a valid property in the array, it will be counted.



But, when you print arr[4], it prints undefined, because JavaScript will return undefined, if you try to access a property which is not defined in an object. For example,



console.log({}['name']);
// undefined


Similarly, since 4 is not yet defined in the arr, undefined is returned.






While we are on this subject, you might want to read these answers as well,




[#65918] Friday, July 3, 2015, 9 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
kevonmoisesf

Total Points: 693
Total Questions: 101
Total Answers: 128

Location: Reunion
Member since Mon, Dec 28, 2020
3 Years ago
kevonmoisesf questions
Sat, Jan 23, 21, 00:00, 3 Years ago
Tue, Feb 18, 20, 00:00, 4 Years ago
Wed, Jun 12, 19, 00:00, 5 Years ago
;