Datasheet

Chapter 1: Introduction to JavaScript
20
If you were to write this to a text file, save it to your hard drive, and load it in your browser, Figure 1 - 7 is
what you would likely see:
Figure 1-7
Let s take a look at the contents quickly to see what is happening.
Breaking it Down
I positioned the script block below the < h1 > heading tag, so the browser executed it after it had rendered
what came before it. The script block itself was ignored by the HTML layout engine, and its contents
were passed on to the JavaScript engine within the browser. Looking at the first line of code:
var today = new Date();
What you see here is known as a statement . Every line of code in JavaScript is called a statement, in fact.
To be precise, you should know that you can put many statements on a line of code, as long as they are
separated by a semicolon. For legibility I have put each statement here on a separate line and have also
used generous indentation something done purely for cosmetic reasons.
Getting back to this particular line of code, I ve used the
var statement to declare a variable. I ll go into
this in more detail in Chapter 2. For now it s enough to know that I declared a variable and assigned a
value to it a new instance of the global
Date object. It just so happens that in JavaScript, when you
create a date and do not say specifically which date , it automatically becomes today s date and time. This
is what I ve done here.
CH001.indd 20CH001.indd 20 6/25/09 7:53:23 PM6/25/09 7:53:23 PM