...

View Full Version : Array



otnj2ee
10-07-2008, 12:59 AM
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.


Thanks

Scott

Trinithis
10-07-2008, 05: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