Sams Teach Yourself JavaScript in 24 Hours

Sams Teach Yourself JavaScript in 24 Hours

By Michael Moncur

Creating Output

You now have two variables—localtime and utctime—which contain the results we want from our script. Of course, these variables don't do us much good unless we can see them. JavaScript includes a number of ways to display information, and one of the simplest is the document.write statement.

The document.write statement displays a text string, a number, or anything else you throw at it. Since your JavaScript program will be used within a Web page, the output will be displayed as part of the page. To display the result, add these statements before the final </script> tag:

document.write("<b>Local time:</b> " + localtime + "<br>");
document.write("<b>UTC time:</b> " + utctime);

These statements tell the browser to add some text to the Web page containing your script. The output will include some brief strings introducing the results, and the contents of the localtime and utctime variables.

Notice the HTML tags, such as <b>, within the quotation marks—since JavaScript's output appears within a Web page, it needs to be formatted using HTML. The <br> tag in the first line ensures that the two times will be displayed on separate lines.

Share ThisShare This

Informit Network