Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2

Thread: Array

  1. #1
    Regular Coder
    Join Date
    Apr 2007
    Posts
    179
    Thanks
    17
    Thanked 0 Times in 0 Posts

    Array

    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

  • #2
    Regular Coder
    Join Date
    Jun 2007
    Location
    USA
    Posts
    527
    Thanks
    26
    Thanked 74 Times in 72 Posts
    Truth be told that arrays (which are exactly the same as objects in JS) index everything by strings. When you do
    Code:
    arr[4] = blah;
    the 4 gets coerced (automatically converted) to a string, "4".
    Trinithis


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •