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 3 of 3
  1. #1
    Senior Coder chump2877's Avatar
    Join Date
    Dec 2004
    Location
    the U.S. of freakin' A.
    Posts
    2,845
    Thanks
    21
    Thanked 157 Times in 148 Posts

    array.length weirdness

    Maybe my brain is still in PHP mode, but why am I getting the following return values for array.length?:

    Code:
    <script type="text/javascript">
    
        var x = new Array;
        alert(x.length);  // Returns "0"
        
        x[12] = 12;
        alert(x.length);  // Returns "13"  -- Why not 1?
        
        
        var y = new Array;
        alert(y.length);  // Returns "0" 
        
        y['12'] = 12;
        alert(y.length);  // Returns "13"  -- Why not 1?
        
        
        var z = new Array;
        alert(z.length);  // Returns "0" 
        
        z['hi'] = 12;
        alert(z.length);  // Returns "0"  -- Why not 1?
        
        
        var a = new Array;
        alert(a.length);  // Returns "0" 
        
        a[12] = "blah";
        alert(a.length);  // Returns "13"  -- Why not 1?
        
    </script>
    Regards, R.J.

    ---------------------------------------------------------

    Help spread the word! Like my YouTube-to-Mp3 Conversion Script on Facebook !! :-)
    [Related videos and tutorials are also available at my YouTube channel and on Dailymotion]
    Get free updates about new software version releases, features, and bug fixes!
    ♪♪ …Need Web Hosting For My YouTube-To-Mp3 Conversion Software? Check Here !!… ♪♪

  • #2
    Senior Coder nikkiH's Avatar
    Join Date
    Jun 2005
    Location
    Near Chicago, IL, USA
    Posts
    1,973
    Thanks
    1
    Thanked 32 Times in 31 Posts
    Arrays in javascript are 0-based and the language is loosely typed. When you use a number as the index, it makes an array. When you use a string, it creates a hash and you must enumerate the elements instead. (for (p in z) alert(p); )

    If you set an element to [12], you have 13 elements, 0-12.
    If you set an element to [12], 0-11 are set to null for you.
    Last edited by nikkiH; 12-22-2006 at 09:30 PM.

    If this post contains any code, I may or may not have tested it. It's probably just example code, so no getting knickers in a bunch over a typo, OK? If it doesn't have basic error checking in it, such as object detection or checking if objects are null before using them, put that in there. I'm giving examples, not typing up your whole app for you. You run code at your own risk.
    Bored? Visit
    http://www.kaelisspace.com/

  • #3
    Senior Coder chump2877's Avatar
    Join Date
    Dec 2004
    Location
    the U.S. of freakin' A.
    Posts
    2,845
    Thanks
    21
    Thanked 157 Times in 148 Posts
    thanks
    Regards, R.J.

    ---------------------------------------------------------

    Help spread the word! Like my YouTube-to-Mp3 Conversion Script on Facebook !! :-)
    [Related videos and tutorials are also available at my YouTube channel and on Dailymotion]
    Get free updates about new software version releases, features, and bug fixes!
    ♪♪ …Need Web Hosting For My YouTube-To-Mp3 Conversion Software? Check Here !!… ♪♪


  •  

    Posting Permissions

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