Tag Archives: JavaScript Array

How to prevent your javaScript code from syntax errors

The Syntax error in the JavaScript code is very common error type when your write the code. Generally when you write the wrong structure of the JavaScript code , browser will give the syntax error at the time of interpretation. Small syntax error in the JavaScript code on a Web page can stop the script from working correctly. Continue reading How to prevent your javaScript code from syntax errors

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>