"Hello World!" Dissected



  • "Hello World!" - The JavaScript Way
  • Language Specification
  • Comments (Hiding From Old Browsers)
  • document.write ()
  • The Semicolon
  • A Step Further
  • Summary




    "Hello World!" - The JavaScript Way

    My first article was a simple introduction to JavaScript. Today we will explore our first JavaScript based web page. Following is a simple script that displays the message "Hello World!." You must copy and paste and then save the above lines in an html file. Load the file in your favorite web browser and experience the magic of JavaScript.

    <html>
    <head>
    <title>Simple JavaScript Example</title>
    </head>
    <body>

    <script language="JavaScript">
    <!--Hiding from old browser
    document.write ("Hello World!");
    // end hiding-->
    </script>

    </body>
    </html>


    Language Specification

    The first thing that we have to do is to tell the browser what scripting language we are going to use. A web page may contain JavaScript, VBScript or both. That's the reason for the specifying the scripting language.


    Comments (Hiding From Old Browsers)

    JavaScript may not be supported by some old web browsers; thus they will ignore the <script> tag. The part in between the <script> and </script> will be treated as normal text and will be displayed in the browser window, which you certainly don't want. To avoid this, you must hide the script between the comments starting with <!-- and ending with //-->.


    document.write ()

    As you might have come to know uptil now, JavaScript is an object based scripting language. By object based we mean that everything in the web browser is considered to be an object with some associated properties and/or methods. The properties and methods of an object are accessed by its name, a dot and the method or property that we want to use. The name of the web page object is document. The method that we want to use here is write. The write property of the document object is used to write something on the web page - in this case "Hello World!." The write method requires that the message to be displayed must be provided to it inside brackets and enclosed within quotation marks. As another example, if I wanted to display the message "Yellow World!," I would have used the statement document.write ("Yellow World!"). I hope that the line document.write ("Hello World!") is clear now.


    The Semicolon

    You may also have noticed one more thing, the semicolon at the end of the document.write ("Hello World!") line. Every statement in JavaScript ends with a semicolon, just as every sentence in English ends with a full stop.


    A Step Further

    The copyright of the article "Hello World!" Dissected in JavaScript is owned by Muhammad Ali Shah. Permission to republish "Hello World!" Dissected in print or online must be granted by the author in writing.

    Go To Page: 1 2

    Articles in this Topic    Discussions in this Topic