Page 202 - Web Applications (803) Class 12
P. 202

cars[2]="Porsche";
                  document.write("<br> After Change the array now is "+"<br>");

                  document.write(cars[0]+"<br>");
                  document.write(cars[1]+"<br>");
                  document.write(cars[2]+"<br>");

                  </script> </font>
                  </body> </html>
              Output of the preceding program is as follows:















                       Do you know?


                      The respective array element is accessed by enclosing the index value in square brackets.


                3.10 ARRAY PROPERTIES AND METHODS

              JavaScript arrays have various built-in properties and methods:


              Length Property
              The length property of an array returns the number of array elements. For example:

                  const fruits = [“pineapple”, “pear”, “jackfruit”, “strawberry”];
                  var lgth = fruits.length;  //This will return 4.
              Sorting an Array

              The sort() method sorts an array in ascending order. For example:
                  const fruits = [“pineapple”, “pear”, “jackfruit”, “strawberry”];
                  fruits.sort();          //will return jackfruit, pear, pineapple, strawberry

              Traversing Through an Array—Using the for loop
              The for loop can be used to iterate over an array and access each array element. For example:

                  <html>
                  <body>
                  <h2>JavaScript Arrays</h2>
                  <p>Using for loop for traversing an array.</p>
                  <font color=blue>

                  <script>
                  const fruits = ["pineapple", "pear", "jackfruit", "strawberry"];


                200   Touchpad Web Applications-XII
   197   198   199   200   201   202   203   204   205   206   207