Section 3: Changing An Object's Properties

Some properties (but not all) can be changed within a Javascript program. These changes can affect what the user sees on your webpage. For example, document.bgColor can be changed, as can window.location. To change a property, we give a command like:


Here, document.bgColor is the name of the property we want to change, and "#FF0000" is the new value we want it to have. This particular command would change the background colour of the screen to red (because "#ff00000" means red to javascript). We could accomplish the same thing using:


Consider this example:


while ( 2+2 != 5 ) {
  document.bgColor = "red" ; /* Change to Red */
  document.bgColor = "green" ; /* Change to Green */
  document.bgColor = "blue" ; /* Change to Blue */
  document.bgColor = "yellow" ; /* Change to Yellow */


<p> Thanks for coming! </p>

In this example, Javascript is told to change the background colour to red, then green, then blue, and then yellow. Furthermore, it is told to keep repeating these instructions as long as (while) 2+2 is not equal (!=) to 5. Since 2+2 is never five, this will keep changing the colours forever! To see this example in action, follow this link.

Notice that the words "Thanks for coming" never appear on the screen. This is because the javascript program never finishes running. It gets "stuck" in the "loop" while ( 2+2 != 5 ) { ... }