SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Wizard
    Join Date
    Jan 2005
    Location
    blahblahblah
    Posts
    1,447
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    count array: length ok?

    Hello,

    I've always used .length to count the elements of an array. I've see people doing it in a much more complex way, though.

    Is length ok, or am I exposed to some security issue?


  2. #2
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,526
    Mentioned
    83 Post(s)
    Tagged
    3 Thread(s)
    Quote Originally Posted by jjshell View Post
    I've always used .length to count the elements of an array. I've see people doing it in a much more complex way, though.

    Is length ok, or am I exposed to some security issue?
    Yes, using .length is the best way to do it when dealing with arrays.

    Perhaps if you show us some of the other complex ways you've seen though, we can help to explain the different circumstances in which they might be of good use.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  3. #3
    SitePoint Guru
    Join Date
    Apr 2006
    Posts
    802
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    //An array can have a length and fewer than that (or no) defined items-
    Code:
    var A= [1, 2, 3];
    A.length= 200;
    
    alert(A.join(', ')+'\n'+A.length);
    /*  returned value: (String)
    1, 2, 3, , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , 
    , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , 
    , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , 
    200
    */

  4. #4
    SitePoint Wizard
    Join Date
    Jan 2005
    Location
    blahblahblah
    Posts
    1,447
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    For example, php.js has quite a complicated approach... But I guess it takes into account objects?


  5. #5
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,526
    Mentioned
    83 Post(s)
    Tagged
    3 Thread(s)
    Quote Originally Posted by jjshell View Post
    For example, php.js has quite a complicated approach... But I guess it takes into account objects?
    It does, yes - because it's trying to simulate in JavaScript the way length can be used in PHP. That's something that's not relevant to normal JavaScript usage.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript


Bookmarks

Posting Permissions

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