Simple JavaScript, Need Explained!

Discussion in 'JavaScript' started by gobbly2100, Jan 19, 2008.

  1. #1
    Hey,

    In this bit of script, the products variable is set as an array and I was wondering what the "products.length" doing exactly? what is it calculating to get the length?
    for (var i = 0; i < products.length; i++) {
      document.write("<option>" + proucts[i].item);
    }
    Code (markup):
    I think this souds really confusing but basicaly I want to know where "products.length" is getting its answer from bearing in mind it is an array?
     
    gobbly2100, Jan 19, 2008 IP
  2. nhl4000

    nhl4000 Well-Known Member

    Messages:
    479
    Likes Received:
    14
    Best Answers:
    0
    Trophy Points:
    110
    #2
    It's the 'length' of the array, the amount of indexes it has.

    And in the code they use the less than sign ("<") because then length property gives the number of indexes in the array, but don't forget an array starts from 0 to x.
     
    nhl4000, Jan 19, 2008 IP
  3. shallowink

    shallowink Well-Known Member

    Messages:
    1,218
    Likes Received:
    64
    Best Answers:
    2
    Trophy Points:
    150
    #3
    Its a property of the array. As such, javascript tracks each time you modify the array. So if you add an element, it increments it. Etc. Close to magic.
     
    shallowink, Jan 19, 2008 IP
  4. gobbly2100

    gobbly2100 Banned

    Messages:
    906
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Awesome, it all suddenly makes sense now.

    Thank you very much guys! :D
     
    gobbly2100, Jan 19, 2008 IP