Along the way you’ll learn:
To understand how to update the date and time on the page, you’ll first have to learn about variables, strings, and functions. Your homework assignment at the end of this chapter will be to figure out how to add seconds to the time.
Variables Store Information
Syntax of Variables
<html><head><title>Seconds in a Day</title>
<!– hide me from older browsers
X var seconds_per_minute = 60; var minutes_per_hour = 60;
var hours_per_day = 24;
Y var seconds_per_day = seconds_per_minute * minutes_per_hour * hours_per_day;
// show me –>
Z </script> </head> <body>
<h1>Know how many seconds are in a day?</h1>
Figure 2-3: Defining and using variables
The first word, var, introduces a variable for the first time, you don’t need to use it after the first instance, no matter how many times you employ the variable in the script.
NOTE: Many people don’t use var in their code. Although most browsers let you get away without it, it’s always a good idea to put var in front of a variable the first time you use it. (You’ll see why when I talk about writing your own functions in Chapter 6.)
If you name your variables descriptively, your code will be easier to under stand while you’re writing it, and much easier to understand when you return to it later for revision or enhancement. Also, no matter which programming language you use, you’ll spend about 50 percent of your coding time finding and getting rid of your mistakes. This is called debugging–and it’s a lot easier to debug code when the variables have descriptive names. You’ll learn more about debugging in Chapter 14.