arrays in javascript

In this example, we learn how to create an array in javascript and how to traverse array. How to perform various built-in operations on array elements, such as sorting, etc.

<html>
<head>
<title></title>
<script language="javascript" type="text/javascript">
var instruments = new Array("piano", "drum", "flute");

//document.write(instruments.length);
//document.write(instruments.toString());

var myarray = new Array();
myarray[0] = "pen";
myarray[1] = "pencil";
myarray[2] = 45;
myarray[3] = 13.6;
/*
for(var i=0; i<myarray.length; i++)
{
document.writeln(myarray[i] + "<br />");
}
*/

myarray[4] = "pakistan";

for(var i=0; i<myarray.length; i++)
{
document.writeln(myarray[i] + "<br />");
}

document.write("<br /> <br />");
myarray.sort();


for(var i=0; i<myarray.length; i++)
{
document.writeln(myarray[i] + "<br />");
}



</script>

</head>
<body>
</body>
</html>

 

Add a Comment

Your email address will not be published. Required fields are marked *