Working With Array Object Using JavaScript

Introduction

 
Array object is used to store multiple values in a single variable. In JavaScript, am array can hold different data types in a single array slot. An array can be constructed in the following ways:
  1. Using the array constructor.
  2. Using the array literal notation.
Using the array constructor:
 
An empty array is constructed in the case when you do not know the exact number of elements to be inserted in the array.
 
Syntax:
  1. varmyArray=new array();  
  2. varmyArray=new array(size);  
Using Literal notion:
 
Array literal notations are a comma-separated list of items enclosed by square brackets.
 
Syntax:
  1. varmyArray=[“element 1”,”element 2”];  
  2. varmyArray=[6,true,false,true];  
Demo:
  1. Create a document called ArrayFunctions.html.
  2. Write the following code:
    1. <!DOCTYPE HTML>  
    2.   
    3. <HEAD>  
    4.     <TITLE>Array Functions</TITLE>  
    5. </HEAD>  
    6.   
    7. <BODY>  
    8.     <SCRIPT type="text/javascript">  
    9.         myarray1 = new Array("Sunday""Monday""Tuesday""Wednesday""Thursday");  
    10.         myarray2 = new Array(1, 2, 3, 4, 5);  
    11.         document.write("Array 1: " + myarray1 + "<br>");  
    12.         document.write("Array 2: " + myarray2 + "<br>");  
    13.         document.write("Concatenated arrays: " + myarray1.concat(myarray2) + "<br>");  
    14.         document.write("Joined two arrays: " + myarray1.join(myarray2) + "<br>");  
    15.         document.write("Array 1 pop out: " + myarray1.pop() + "<br>");  
    16.         document.write("Reversed array 1: " + myarray1.reverse() + "<br>");  
    17.         document.write("Shifted array 1: " + myarray1.shift() + "<br>");  
    18.         document.write("Sorted array 1: " + myarray1.sort() + "<br>");  
    19.         document.write("Array 2 in string format: " + myarray2.toString() + "<br>");  
    20.     </SCRIPT>  
    21. </BODY>  
    22.   
    23. </HTML>  
  3. Execute the script by Opening the file in the Web Browser. If you are using Internet Explore click on “Allow Blocked Content” to allow the script to execute and you are using Mozilla Firefox then click on allow “ActiveX Controls”.
     
    ActiveX Controls
Thank you for reading this article. If you need any help feel free to contact me.