Tag Archives: JavaScript Array

How to assign undefined value to an array element in JavaScript

 
Following example shows that how to assign undefined value to an array element in JavaScript.

<HTML>
   <HEAD>
   <TITLE>
     undefined element in javaScript array
   </TITLE>
   </HEAD>
   <BODY>
   <SCRIPT>
      var ArrayCity = new Array(4);
         ArrayCity[0] = "New Delhi";
         ArrayCity[1] = "NewYork";
         ArrayCity[2] =  undefined;
         ArrayCity[3] = "Kolkata";
 
     for (var i = 0; i < ArrayCity.length; i++){
       if (ArrayCity[i] != undefined)
        document.write("ArrayCity[" + i + "] = " + ArrayCity[i] + "<br>");
     }     
   </SCRIPT>
   </BODY>
</HTML>

Output of above example will be:

ArrayCity[0] = New Delhi
ArrayCity[1] = NewYork
ArrayCity[3] = Kolkata

How to pass an array to a function in JavaScript

 
This example shows that how to pass an array to a function in javaScript :

<HTML>
   <HEAD>
   <TITLE>
   Iteration Two
   </TITLE>
   <SCRIPT>
      function showArrayOfCities(tempArray)
       {
         var quote = "";
         for (var i = 0; i < tempArray.length; i++)
           {
             quote += tempArray[i]+"</br>";
           }
         return quote;    
       }
   </SCRIPT>
   </HEAD>
   <BODY>
   <SCRIPT>
       var ArrayCity = new Array(4);
         ArrayCity[0] = "New Delhi";
         ArrayCity[1] = "NewYork";
         ArrayCity[2] = "London";
         ArrayCity[3] = "Kolkata";        
       document.write(showArrayOfCities(ArrayCity));
   </SCRIPT>
   </BODY>
</HTML>