Run Your First JavaScript Program

Published: September 21, 2011
Updated: October 30, 2011

In the movie 2010 a Space Odyssey, when HAL begins to loose his memory he immediately starts reciting the song Daisy, Daisy. What was the significance of this song. As it turns out, teaching a computer to recite Daisy, Daisy was one of the first escapades of the original members of the hacker movement.

This reflection caused the author to recall the many programming classes taken over the course of an academic career that spanned over three decades. In most every introductory programming class taught to modern students, the first program that a learner develops is the Hello World program.

The purpose for writing this program is not to accomplish anything particularly unique; how many ways are there to direct a computer to display the phrase "Hello World" without using any special formatting techniques? That was a rhetorical question because the obvious answer is ... not many.

The purpose for writing the Hello World program is to simply lead the learner to discover how to generate output on a computer's display, regardless of the particular programming language studied. The program is not particularly difficult to develop. To display the phrase typically takes about one line of code in most programming languages. However, the preliminaries may add a few steps to the endeavor.

As an example, consider the code necessary to display Hello World in JavaScript.

The following code snippet specifies one method to accomplish the task:

document.write("Hello World");

JavaScript is Object Based

The above method to display "Hello World" on the screen demonstrates the object based nature of JavaScript. Object Oriented Programming (OOP) was introduced to more closely associate programming with how people see the world. People see objects, such as cars, TVs, and flowers. Earlier languages were procedural based, which does not associate well with the real world.

The author stated that JavaScript is object-based, not object-oriented, why the difference?

To maintain the relative simplicity of JavaScript, the developers of the language did not include all the functionality of object-oriented languages. Therefore, JavaScript is object-based because JavaScript's ability to access system resources is limited as a security precaution. JavaScript runs in a web browser so security was a concern for the language's implementers. Now, on to the code...

In the above programming line, document represents an object. Objects contain methods or actions that may be performed on those objects, and attributes or descriptors of the objects. The document object above includes the write method, which writes information to the Web page. The statement document.write("Hello World") takes the phrase Hello World and displays the phrase on the Web page, which happens to be the document.

Note: Using the document.write method after a Web page loads will cause the Web browser to create a new page to display whatever the browser is directed to display by the document.write() method.

JavaScript runs in a Web Browser

The preceding code will not run on its own, however ... something is missing.

In order for a program written in JavaScript to run, it must be embedded within a web page or an HTML document because JavaScript runs within a web browser, such as Internet Explorer X.

The following code snippet illustrates what is necessary to run the Hello World program in JacaScript, complete with the necessary HTML overhead:

<html>
  <head>
    <title>Hello World</title>
  </head>

  <body>
    <script type="text/javascript>
      document.write("Hello World");
    </script>
  </body>
</html>

HTML is Tag-Based

The above snippet demonstrates the overhead involved in HTML and JavaScript programming. HTML is a tag-based language and most of the tags in the above program do not directly relate to displaying the message Hello World but but identify to the browser what elements are contained in the document.

The hello.html file contains five tag-pairs. These pairs are as follow:

  • <html>...</html>
  • <head>...</head>
  • <title>...</title>
  • <body>...</body>
  • <script>...</script>

The two tag-pairs above that directly relate to displaying the message are the <body> <script>...</script> </body> tag pairs, which identify JavaScript as a sort of program to run within the document and display the message.

Each HTML tag-pair comprise an opening and closing tag. The opening tag identifies a directive to the browser and the closing tag, preceded by a "/" character, delineates the end of the pair. Each tag is enclosed within the left and right carot characters "<" and ">". Once a tag is specified, the directive remains in effect until the browser encounters the closing tag so a tag pair may envelope other tag pairs.

In order to run the above program, you need to create a HTML file, which includes the above code; save the file with an extension .htm or .html, then open the saved file in a web browser.

If you have never written a program and would like to see the above code in action, simply open up notepad then copy the above code and paste it into your notepad screen. Save the file as hello.html then open the file you created. Your default browser will open and display "Hello World."

More by this Author


Comments

No comments yet.

    Sign in or sign up and post using a HubPages Network account.

    0 of 8192 characters used
    Post Comment

    No HTML is allowed in comments, but URLs will be hyperlinked. Comments are not for promoting your articles or other sites.


    Click to Rate This Article
    working