Saturday 19 November 2011

Javascript Array


·         The array object is used to store a set of values in a single variable name.
·     Arrays are created using a constructor, just like other objects. The Array() constructor uses the following format.

variablename = new Array(); or variablename = new Array([size]);

·         There are two ways of adding values to an array.

Example-1

<html>
          <body>
                   <script language="javascript">
                             var arr = new Array();
                             arr[0] = “PHP”;
                             arr[1] = “VB”;
                             arr[2] = “C++”;
                             for(x in arr)
                             {
                                      document.write(arr[x]+”<br/>”);
                             }
</script>
          </body>
     </html>

 Example-2

<html>
          <body>
                   <script language="javascript">
                             var arr = new Array(“php”,”vb”,”c++”);
for(x in arr)
                             {
                                      document.write(arr[x]+”<br/>”);
                             }
</script>
          </body>
          </html>

Ø       Accessing Arrays

·     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.

document.write(arr[0]);

Ø       Modify values in existing Arrays

To modify a value in an existing array, just add a new value to the array with a specified index number.

arr[2] = “sad”;
document.write(arr[2]);

Posted By : Hemangi Zala

No comments:

Post a Comment