Array Problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ziycon
    Contributor
    • Sep 2008
    • 384

    Array Problem

    I have a variable called num and i want to pass it into the array element number but i get undefined showing on the page, heres what i mean:

    If i put 1 into the document.write( array[1]); then it works fine??
    Code:
    var num = 1;
    
    array[1] = 'Test';
    
    document.write(array[num]);
  • mrhoo
    Contributor
    • Jun 2006
    • 428

    #2
    define array

    Code:
    var num = 1, array=[];
    
    array[1] = 'Test';
    
    alert(array[num]);

    Comment

    • dmjpro
      Top Contributor
      • Jan 2007
      • 2476

      #3
      Originally posted by ziycon
      I have a variable called num and i want to pass it into the array element number but i get undefined showing on the page, heres what i mean:

      If i put 1 into the document.write( array[1]); then it works fine??
      Code:
      var num = 1;
      
      array[1] = 'Test';
      
      document.write(array[num]);
      How does your "array[1]" work fine? Neither "array[1]" nor "array[num]" is working.
      Array is not a primary data type, so you need to define before you use it.

      Comment

      Working...