I was reading though Javascript, the definitive guide and came across a paragraph which said and I quote
If you index an array with a string that happens to be a non-negative integer,it behaves as an array index not an object property
So something like a["4"] would be stored at an index of 5, whereas something like a["-1.26"] would be stored as a property "1.26"..
I got a little adventurous and tried a["02"] = 2;
Now I can set it, retrieve it but it's neither set as a[2] (basically a[parseInt("02")]) , nor can I get it when I print the array ..
Here is my code. I tried it with node and the browser.
> a[3] = 3;
3
> a["-1.2"] = 10;
10
> a
[ , , , 3, '-1.2': 10 ]
> a["02"] = 2;
2
> a
[ , , , 3, '-1.2': 10 ]
> a["02"]
2
> a.length
4
I just want to understand what exactly is happening.
/^0|[1-9][0-9]*$/is treated as a number.a["+1"]isn'ta[1]