Previous Example-|-Next Example-|-Return to Chapter Listing

Example 9.1:
Creating and Checking an Array

Click on the buttons to see what is held in the specified positions in the arrays. What you see for the second to last button depends on the version of the browser you're using. In Netscape Navigator 2 you'll see 187--the array length, the number passed to the array in parentheses. In Netscape Navigator 3 and later, and in Internet Explorer 3, you'll see null, because in those browsers the [0] position in the array no longer holds the length.


This is the script we used in the HEAD: <SCRIPT LANGUAGE="JAVASCRIPT"> <!-- function makeArray(n) { this.length = n for (var i=1; i <= n; i++){ this[i] = null; } return this } var acode = new makeArray(187) var area = new makeArray(187) area[1] = "Alabama "; acode[1] = 205 area[2] = "Alabama "; acode[2] = 334 area[3] = "Alaska "; acode[3] = 907 area[4] = "Alberta "; acode[4] = 403 area[5] = "Antigua "; acode[5] = 268 area[6] = "Arizona "; acode[6] = 520 area[7] = "Arizona "; acode[7] = 602 area[8] = "Arkansas "; acode[8] = 501 area[9] = "Bahamas "; acode[9] = 242 area[10] = "Barbados "; acode[10] = 246 area[11] = "Bermuda "; acode[11] = 441 area[12] = "British Columbia "; acode[12] = 250 area[13] = "British Columbia "; acode[13] = 604 //--> </SCRIPT> And this form: <FORM> <INPUT TYPE="BUTTON" VALUE="What's in area Position 12?" onclick="alert(area[12])"><P> <INPUT TYPE="BUTTON" VALUE="What's in acode Position 12?" onclick="alert(acode[12])"><P> <INPUT TYPE="BUTTON" VALUE="What's in area Position 110?" onclick="alert(area[110])"><P> <INPUT TYPE="BUTTON" VALUE="What's in area Position 0?" onclick="alert(area[0])"><P> <INPUT TYPE="BUTTON" VALUE="What's the area length (area.length)?" onclick="alert(area.length)"><P> </FORM>
Previous Example-|-Next Example-|-Return to Chapter Listing