Array
What need Array?

Suppose we want to store 100 data item. So we need 100 different variable. So this is very typical work for programmer to manage 100 different variable.To overcome this problem we create a special type variable known as array.

Array:-

array is a special type of variable. that store one more values in one single variable. And each element in the array has its own index.

How to create Array?
var country=new Array("INDIA","UK","US");
or

var country=new Array();
country[0]="INDIA";
country[1]="US";
country[2]="UK";

Example 1:- To use of Array
<html>
<head>
<title>My Javascript Code</title>
</head>
<body>
<script language="javascript" type="text/javascript">
var x;
var country=new Array("INDIA","UK","US");
for(x in country)
{
document.write(country[x]+"<br>");
}
</script>
</body>
</html>
Example 2:- To use of array

<html>
<head>
<title>My Javascript Code</title>
</head>
<body>
<script language="javascript" type="text/javascript">
var x;
var country=new Array();
country[0]="INDIA";
country[1]="US";
country[2]="UK";
for(x=0;x<3;x++)
{
document.write(country[x]+"<br>");
}
</script>
</body>
</html>

Example 3:-

<html>
<head>
<title>My Javascript Code</title>
</head>
<body>
<script language="javascript" type="text/javascript">
var x;
var names=new Array();
names[0]="Raj";
names[1]="Amit";
names[2]="Vikas";
document.write("Unsorted Array<br>");
for(x=0;x<3;x++)
{
document.write(names[x]+"<br>");
}
document.write("Sorted Array<br>");
document.write(names.sort());
</script>
</body>
</html>


Previous
Next