Lesson Three: Assigning Values to Variables

  1. A variable is used to reserve a storage space inside the memory of the computer. It is like a drawer in a cabinet. The drawer being the memory space and the cabinet being the computer's memory.
  2. Create a variable in your JavaScript file by putting this code above the document.write("488 Million Years Ago the Mollusks Appeared"); code:
    var event1;
  3. The var event1; code establishes a space in memory with the label of event1.
  4. Enter this code to insert text into the event1 variable memory space:
      event1 = "488 Million Years Ago the Mollusks Appeared";
  5. Now when you refer to the variable event1, the browser will use the text that is stored in memory ("488 Million Years Ago the Mollusks Appeared".)
  6. Substitute the event1 variable for the text that is currently in the document.write() parenthesis.
  7. Your JavaScript code should now look like this:
    var event1;
    event1 = "488 Million Years Ago the Mollusks Appeared";
    document.write(event1);
  8. Save the JavaScript file and test your program. You should get the same result that you got in lesson two only this time you achieved the results using a variable: Sample

Lesson Four: More Variables