JavaScript : Array Object [1/2]

An array is a special variable, which can hold more than one value, at a time. You can refer to a particular element in an array by referring to the name of the array and the index number.

Syntax 1
    ArrayName = new Array();
    ArrayName [0] ="value1";
    ArrayName [1] ="value2";
   ...
    ArrayName [N] ="valueN";

Syntax 2
    ArrayName  = new Array( value1",value2" ... ,valueN");

Syntax 3
     ArrayName   = [value1",value2" ... ,valueN"];

Retrieve Value 1
    ArrayName  ArrayName[0];
    ArrayName +=  ArrayName[1];
   ...
    ArrayName +=  ArrayName[N];

Retrieve Value 2
   for (variableName in  ArrayName ) {
      ArrayName[variableName];
   }

Example with Date
<script language="JavaScript">
   var DayName = new Array("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday");
   var d = new Date();
   var output = "Today is " + DayName[d.getDay()] + " ";
   output += d.getDate()+ "/";
   output += (d.getMonth() + 1) + "/";
   output += d.getYear() + " ";
   output += d.getHours() + ":";
   output += d.getMinutes() + ":";
   output += d.getSeconds();
   document.write(output);
</script>


figure 1 : Sample with Date

##############################

JavaScript : Date Object << Previous Chapter || Next Chapter >> JavaScript Array Object [2/2]

No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...