HTML and CSS Reference
In-Depth Information
The property most used with arrays is the length property, which determines the
number of elements in the array, that is, the size of the array. You can also use this prop-
erty to set the size of an array; for example, if you set the length to 0, all elements of the
array will be removed.
Table 9.1 Array Properties
Property
What It Does
constructor
References the object's constructor.
length
Returns the number of elements in the array.
prototype
Extends the definition of the array by adding properties and
methods.
EXAMPLE 9.5
<html>
<head><title>Array Properties</title>
<h2>Array Properties</h2>
<script type="text/javascript">
1
var book = new Array() ; // Create an Array object
book[0] = "War and Peace"; // Assign values to elements
book[1] = "Huckleberry Finn";
book[2] = "The Return of the Native";
book[3] = "A Christmas Carol";
book[4] = "The Yearling";
book[5] = "Exodus";
</script>
</head>
<body bgcolor="lightblue">
<big>
<script type="text/javascript">
2
document.write("The book array has " + book.length +
" elements<br />");
document.write("The book's constructor is: "+
3
book.constructor + "<br />");
4
book.length=0;
document.write("The book has been trashed! The first book is"+
5
book[0] );
6
document.write("<br />The size is now "+ book.length );
</script>
</big>
</body>
</html>
 
Search WWH ::




Custom Search