JavaScript's data types can be combined to create more sophisticated structures.

JavaScript functions are an important method for organizing your scripts.


document.write(typeof quadranomial)

Note that this is different than:

document.write(typeof quadranomial(1, 2, 3, 4))
document.write(typeof void quadranomial(1, 2, 3, 4))

The precedence and associativity table previously described the void operator.

JavaScript objects are the key to interacting with documents.


<script language="JavaScript">
  function checkCollege(college) {
    if (college.value.toLowerCase() != 'amherst College' &&
      college.value.toLowerCase() != 'amherst') {
      college.value = 'Not Acceptable!'
    } else {
      college.value = 'Amherst College'
    }
  }
</script>

<form name="CollegeInput">
  <input name="College" type="text" value="Type Your College Here"
         size="22" onChange="checkCollege(this)">
</form>
   

This script can be interpreted as follows:

document.writeln(typeof document.CollegeInput)
document.write(typeof document.CollegeInput.College)
document.CollegeInput.College.not = 'Not Acceptable!'
document.CollegeInput.College.checkCollege = checkCollege()

Note also that some properties in a predefined object might be read-only.

Netscape has published a nice comparison of objects in C++/Java and JavaScript.

ssCellA1 = { column:0, row:0, name:cr2name, value:32.4 }

document.write('<table width="150" border="1" cellspacing="1" cellpadding="1">')
document.write('<tr><td width="50" align="center"></td>')
document.write('<td width="100" align="center"><b>A</b></td>')
document.write('</tr><tr><td align="center"><b>1</b></td>')
document.write('<td align="center">')
document.write(ssCellA1.name() + ' == ' + ssCellA1.value)
document.write('</td></tr></table>')
ssCellB1 = new Object()
ssCellB1.column
= 1
ssCellB1.row
= 0
ssCellB1.name
= cr2name
ssCellB1.value
= ''

document.write('<table width="250" border="1" cellspacing="1" cellpadding="1">')
document.write('<tr><td width="50" align="center"></td>')
document.write('<td width="100" align="center"><b>A</b></td>')
document.write('<td width="100" align="center"><b>B</b></td>')
document.write('</tr><tr><td align="center"><b>1</b></td>')
document.write('<td align="center">')
document.write(ssCellA1.name() + ' == ' + ssCellA1.value)
document.write('</td><td align="center">')
document.write(ssCellB1.name() + ' == ' + ssCellB1.value)
document.write('</td></tr></table>')
function SSCell(column, row, value) {
  this.column = column
  this.row = row
  this.name = cr2name
  this.value = value
}
ssCellA2 = new SSCell(0, 1, -27)
ssCellB2 = new SSCell(1, 1, 7.5)

document.write('<table width="250" border="1" cellspacing="1" cellpadding="1">')
document.write('<tr><td width="50" align="center"></td>')
document.write('<td width="100" align="center"><b>A</b></td>')
document.write('<td width="100" align="center"><b>B</b></td>')
document.write('</tr><tr><td align="center"><b>1</b></td>')
document.write('<td align="center">')
document.write(ssCellA1.name() + ' == ' + ssCellA1.value)
document.write('</td><td align="center">')
document.write(ssCellB1.name() + ' == ' + ssCellB1.value)
document.write('</tr><tr><td align="center"><b>2</b></td>')
document.write('<td align="center">')
document.write(ssCellA2.name() + ' == ' + ssCellA2.value)
document.write('</td><td align="center">')
document.write(ssCellB2.name() + ' == ' + ssCellB2.value)
document.write('</td></tr></table>')
 
document.write('The members of the object ssCellA1 are:<br>')
for (member in ssCellA1) {
  document.write('ssCellA1.' + member + ' = ' + ssCellA1[member] + '<br>')
}

Note that in this statement, the variable member is set to the name of each of the object's members as a string.

JavaScript does not have an explicit array type, but you can create an array object.


Even JavaScript's core data types and functions are objects in their own right!


Function objects have some additional useful properties and methods.


To work with JavaScript, no special tools are required, just a text editor to write scripts into a .html file (the source view of Dreamweaver can also be used), and a web browser to view the result.


  1. For the Spreadsheet Object, modify its special version of toString so that it outputs a form in each cell of the spreadsheet. Then modify the spreadsheet so that an entry in each form changes the corresponding value stored in the spreadsheet. Add a method to check the values in the spreadsheet independently from the form.