The Length of an Array

Earlier, you read that the length of an array is hidden away within the property length. length is a property of the array, and though it may be hidden, it isn't beyond your reach.

To find out how many elements are in the days_of_week array, you can use the following:

days_of_week.length

In the following example, an alert box is used to show this value:

<html>
<head>
<title>A Simple Page</title>
<script language="JavaScript">
<!-- Cloaking device on!
var days_of_week = new

Array("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday");
alert(days_of_week.length);
// Cloaking device off -->
</script>
</head>
<body>

</body>
</html>

The output from this example is shown in Figure 5.5.

Figure 5.5. Alert box ...

Get JavaScript™ 1.5 by Example now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.