Page 147 - DCAP408_WEB_PROGRAMMING
P. 147

Unit 5: Scripting Language




          Each element in the array has its own ID so that it can be easily accessed.           Notes
              Create an Array
               An array can be defined in three ways.
               The following code creates an Array object called myCars:
               1:
               var myCars=new Array(); // regular array (add an optional integer
               myCars[0]=”Mercedes”;       // argument to control array’s size)
               myCars[1]=”Ferari”;

               myCars[2]=”BMW”;
               2:
               var myCars=new Array(“Mercedes”,”Ferari”,”BMW”); // condensed array
               3:
               var myCars=[“Mercedes”,”Ferari”,”BMW”]; // literal array




             Notes  Note: If you specify numbers or true/false values inside the array then the variable
             type will be Number or Boolean, instead of String.
              Access an Array
               You can refer to a particular element in an array by referring to the name of the array and
               the index number. The index number starts at 0.
               The following code line:
               document.write(myCars[0]);
               Will result in the following output:

               Mercedes
              Modify Values in an Array
               To modify a value in an existing array, just add a new value to the array with a specified
               Index number:
               myCars[0]=”Opel”;
               Now, the following code line:
               document.write(myCars[0]);

               will result in the following output:
          Opel

                                     Table 5.3:  Array Object  Methods

                Method        Description
                concat()      Joins two or more arrays, and returns a copy of the joined arrays
                join()        Joins all elements of an array into a string
                                                                                Contd...



                                           LOVELY PROFESSIONAL UNIVERSITY                                   141
   142   143   144   145   146   147   148   149   150   151   152