Definition : The Array object is used to store multiple values in a single variable.
The following code creates an Array object called myFruits:
1.
var myFruits=new Array(); // regular array (add an optional integer
myFruits[0]="Mango"; // argument to control array's size)
myFruits[1]="Orange";
myFruits[2]="Pineapple";
2.
var myFruits=new Array("Mango","Orange","Pineapple"); // condensed array
3.
var myFruits=["Mango","Orange","Pineapple"]; // literal array
Note: If you specify numbers or true/false values inside the array then the variable type will be Number or Boolean, instead of String.
Example:
HTML Body tag:
<body>
<form id="form1" runat="server">
<div>
<input id="btnCreateArray" type="submit" value="Create Array" name="btnCreateArray" onclick="CreateArray();"/>
</div>
</form>
</body>
JavaScript Code:
<script type="text/javascript" language="javascript">
var myarray = new Array();
function CreateArray()
{
myarray[0]= "apple";
myarray[1]="orange";
myarray[2] ="grapes";
document.write(myarray[0] + " " + myarray[1] + " " + myarray[2]);
}
CreateArray();
</script>
Create an Array
An array can be defined in three ways. The following code creates an Array object called myFruits:
1.
var myFruits=new Array(); // regular array (add an optional integer
myFruits[0]="Mango"; // argument to control array's size)
myFruits[1]="Orange";
myFruits[2]="Pineapple";
2.
var myFruits=new Array("Mango","Orange","Pineapple"); // condensed array
3.
var myFruits=["Mango","Orange","Pineapple"]; // literal array
Note: If you specify numbers or true/false values inside the array then the variable type will be Number or Boolean, instead of String.
Example:
HTML Body tag:
<body>
<form id="form1" runat="server">
<div>
<input id="btnCreateArray" type="submit" value="Create Array" name="btnCreateArray" onclick="CreateArray();"/>
</div>
</form>
</body>
JavaScript Code:
<script type="text/javascript" language="javascript">
var myarray = new Array();
function CreateArray()
{
myarray[0]= "apple";
myarray[1]="orange";
myarray[2] ="grapes";
document.write(myarray[0] + " " + myarray[1] + " " + myarray[2]);
}
CreateArray();
</script>
No comments:
Post a Comment