JavaScript Tutorial/Array/Array Declaration
Содержание
- 1 Array declaration with element count
- 2 Define two arrays and use for statement to ouput their values
- 3 Initialize a string array during declaration and check the array size
- 4 Integer Array Declaration with initialization
- 5 Output array element in a HTML table format
- 6 Paralled array
- 7 Store strings in array
Array declaration with element count
<html>
<head>
<title>A Simple Page</title>
<script language="javascript">
<!--
var days_of_week = new Array(7);
days_of_week[0] = "Sunday";
days_of_week[1] = "Monday";
days_of_week[2] = "Tuesday";
days_of_week[3] = "Wednesday";
days_of_week[4] = "Thursday";
days_of_week[5] = "Friday";
days_of_week[6] = "Saturday";
var x = 2;
alert(days_of_week[x]);
// -->
</script>
</head>
<body>
</body>
</html>
Define two arrays and use for statement to ouput their values
<html>
<head>
<title>Define two arrays and use for statement to ouput their values</title>
</head>
<body>
<h1>Define two arrays and use for statement to ouput their values</h1>
<table border="1">
<script language="javascript" type="text/javascript">
<!--
var myArray = new Array();
myArray[0] = "A";
myArray[1] = "B";
myArray[2] = "C";
myArray[3] = "D";
myArray[4] = "E";
var myArray2 = new Array();
myArray2[0] = "1";
myArray2[1] = "2";
myArray2[2] = "3";
myArray2[3] = "4";
myArray2[4] = "5";
for (var i=0; i<myArray.length; i++) {
document.write("<tr><td>" + myArray[i] + "</td>");
document.write("<td>" + myArray2[i] + "</td></tr>");
}
//-->
</script>
</table>
</body>
</html>
Initialize a string array during declaration and check the array size
<html>
<head>
<title>A Simple Page</title>
<script language="javascript">
<!--
var days_of_week = new Array("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday");
alert(days_of_week.length);
// -->
</script>
</head>
<body>
</body>
</html>
Integer Array Declaration with initialization
<html>
<head>
<title>Array Declaration</title>
<script language="javascript" type="text/javascript">
<!--
var x = new Array(2,3,4);
alert(x);
//-->
</script>
</head>
<body>
</body>
</html>
Output array element in a HTML table format
<html>
<head>
<title>Table of Phone Numbers</title>
</head>
<body>
<h1>Table of Phone Numbers</h1>
<table border="1">
<script language="javascript" type="text/javascript">
<!--
var myArray = new Array();
myArray[0] = "1";
myArray[1] = "2";
myArray[2] = "3";
myArray[3] = "4";
myArray[4] = "5";
for (var i=0; i<5; i++) {
document.write("<tr><td>Number " + i + " is:</td>");
document.write("<td>" + myArray[i] + "</td></tr>");
}
//-->
</script>
</table>
</body>
</html>
Paralled array
<html>
<head>
<title>Define two arrays and use for statement to ouput their values</title>
</head>
<body>
<h1>Define two arrays and use for statement to ouput their values</h1>
<table border="1">
<script language="javascript" type="text/javascript">
<!--
var myArray = new Array();
myArray[0] = "A";
myArray[1] = "B";
myArray[2] = "C";
myArray[3] = "D";
myArray[4] = "E";
var myArray2 = new Array();
myArray2[0] = "1";
myArray2[1] = "2";
myArray2[2] = "3";
myArray2[3] = "4";
myArray2[4] = "5";
for (var i=0; i<myArray.length; i++) {
document.write("<tr><td>" + myArray[i] + "</td>");
document.write("<td>" + myArray2[i] + "</td></tr>");
}
//-->
</script>
</table>
</body>
</html>
Store strings in array
<html>
<head>
<title>Store strings in array</title>
</head>
<body>
<P>
<script language="javascript" type="text/javascript">
<!--
var myArray = new Array();
myArray[0] = "AAA";
myArray[1] = "BBB";
myArray[2] = "CCC";
for (var i=0; i<myArray.length; i++) {
document.write("Element " +i+ " contains: " +myArray[i]+ "<br />");
}
//-->
</script>
</p>
</body>
</html>