In my previous article, A First JavaScript Program, we introduced the hello_world.html program (click here to try it). In this article we look at it again. This time, the code is color coded by function and if you click on a bit of code, it is linked to short explanations and references to in-depth explainations. The color code is explained after the program.
<html>
<head><title>
Hello World in JavaScript
</title></head>
<body>
<script type="text/javascript">
<!--
// get the current date and time
var now = new Date()
// Check the time
if ( now.getHours() < 12 )
{
// It's before noon
// print "Good Morning World!" in the window
document.write("Good Morning World!")
}
else
{
// It's after noon
// just print "Hello World!" in the window
document.write("Hello World!")
}
//-->
</script>
<noscript>
Hi Non-JavaScript User!
</noscript>
</body></html>
| Color | Represents |
|---|---|
| green | html tags |
| black | text |
| aqua | comments |
| lime | control flow |
| olive | objects and methods |
| maroon | test |
| silver | variables |
HTML Tags
- These are html tags. If you need a refresher on html, check out
the HTML resources in my article
Getting Ready to Learn JavaScript.
- The <script> tag is used to tell the web browser that information is a script and not text or html to be displayed.
My article on Using the Script and Noscript Tags (COMING SOON)
- The <noscript> tag is used to provide information for viewers whose browsers do not or are not providing script support.
My article on Using the Script and Noscript Tags (COMING SOON)
Text
This is text that will be displayed directly by the browser.
This is text that may be displayed by the JavaScript script.
Comment
// is one type of JavaScript comments. More information on Comments in JavaScript
<!-- and --> are html comments, they are used to wrap JavaScript within <script> tags to hide it from older browsers that don't support the script tag.
My article on Using the Script and Noscript Tags (COMING SOON)
Flow Control
The if ... else ... statement is one type of flow control in JavaScript. More information on the if statement
Objects and Methods
Objects and Methods are where most of the action in JavaScript takes place.
Date Object and methods
Window Object and methodsThese are examples of the document.write() method, more about this method is available here.
Variables
- "now" is the name of a variable you have defined. A variable is used to hold information of your choice. var is a keyword that tells your program that you are defining a new variable. You also have to tell the program what kind of information you are going to hold in the variable, in this case, it is a date object.


















