View Full Version : Array

10-06-2008, 11:59 PM
Usually, to create an array:

var testAry = new Array();

testAry[0] ="abc";
testAry[1] ="abc1";
testAry[2] ="abc2";

But I also see:

var testAry = new Array();

testAry["abc"] ="abc";
testAry["abc1"] ="abc1";
testAry["abc2"] ="abc2";

Why is a string allowed as an index? I believe the number should only be allowed.



10-07-2008, 04:15 AM
Truth be told that arrays (which are exactly the same as objects in JS) index everything by strings. When you do

arr[4] = blah;

the 4 gets coerced (automatically converted) to a string, "4".

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum