We'll start with things that are non-interactive, things such as printing the date at the top of the page.

To print something to the page, you use the document.write() method. Here's an example:



Which does this: Whatever is between the parenthesis will be printed to the screen. The quotes tell the browser that it should interpret what's between them literally.
Notice that I print a

at each end. That's because the JavaScript is writing to the page in HTML. If you want a line break, you must explicitly print one.
Also, when you do a write, it will continue printing where it left off. So,



is the same as



See! JavaScript has many function available for getting environmental variables, like the date, the time, etc.
Here's how you'd include the current date in your page:



Which does this: The + in this case means that the "strings" of text should be added together.

You can also pick out specific parts of the date and time. To get just the day, you'd do this:



Which does this: Here's a list of useful date/time functions, and what they do:

getDate() Returns the current day of the month
getDay() Returns the current day of the week, from 0 to 6
getHours() Return the current hour
getMinutes() Returns the current minutes
getMonth() Returns the month, from 0 to 11
getSeconds() Returns the current seconds
getYear() Returns the year

Every one of the above methods (That's what they're called.) is called through a Date object, like I did in the day/month example. All Date objects are initialized with the current date and time when they are created.
For getDay() and getMonth(), the number returned is one less than what you'll usually want, so you'll need to add 1, as in the previous example.

Here's a few more examples:





Which does this:



Which does this: That should give you a pretty good idea of what JavaScript can do statically. Now we'll move on to our first experiment with interactive code!

The next lesson is Buttons.

Return to the Menu