Why using strings as keys of array, console is showing that array without these declared values and while iterating by this values where keys are string aren't displayed? , although i can get value of them.

>> var arr = [ 0, 1, 2, 3 ];
   undefined

>> arr["something"] = "aught";
   "aught"

>> arr
   [0, 1, 2, 3]

>> arr["something"]
   "aught"

>> for( var i = arr.length; i--; console.log( arr[ i ] ) );
   3
   2
   1
   0

I understand that arrays are objects which has implemented some kind of 'enumerate' interface in javascript's engine. Most interesting is that interpreter isn't throwing either warning or error so i spent a few of time of searching for where the data could be lost. I now, I was wrong and I used [] instead of {}

share|improve this question
1  
You're probably coming from PHP background, is that right? – Sergio Tulentsev Dec 25 '11 at 16:39

2 Answers

up vote 4 down vote accepted

In javascript there are 2 type of arrays: standard arrays and associative arrays

  • [ ] - standard array - 0 based integer indexes only
  • { } - associative array - javascript objects where keys can be any strings

So when you define:

var arr = [ 0, 1, 2, 3 ];

you are defining a standard array where indexes can only be integers. When you do arr["something"] since something (which is what you use as index) is not an integer you are basically defining a property to the arr object (everything is object in javascript). But you are not adding an element to the standard array.

share|improve this answer
1  
@abuduba, because there is nothing wrong :-) Your code is perfectly valid javascript. It's ambiguous to the reader since you are mixing the 2 type of arrays but it is valid. Why would you want the interpreter to indicate that something is wrong when there is nothing wrong. – Darin Dimitrov Dec 25 '11 at 16:44
4  
This is mostly a terminology issue, but javascript does NOT generally call {} an associative array. They call it an object that has properties. Don't most people agree that it's best not to confuse things by calling a javascript object an associative array? – jfriend00 Dec 25 '11 at 16:46
1  
Note: If you try to use defineProperty on the array like Object.defineProperty.call(arr,'something','aught'); it will throw an error. – David Dec 25 '11 at 16:47
You`re right. Key something is property of object instead index of array => arr.something. Maybe I'am too long without sleep .. I forgot that. Thanks @David - Nice, i did`nt know about it – abuduba Dec 25 '11 at 16:50
1  
"everything is object in javascript" Why do people say this? Not everything is an object. – am not i am Dec 25 '11 at 16:56
show 1 more comment

for( var i = arr.length; i--; console.log( arr[ i ] ) );

This will only give you the numeric indices, of course, but you can still loop over both numeric indices and string keys of your array like this:

for (var x in arr) {
    console.log(x + ": " + arr[x]);
}
/* (console output):
     0: 0
     1: 1
     2: 2
     3: 3
     something: aught
*/
share|improve this answer

Your Answer

 
or
required, but never shown
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.