ArtsAutosBooksBusinessEducationEntertainmentFamilyFashionFoodGamesGenderHealthHolidaysHomeHubPagesPersonal FinancePetsPoliticsReligionSportsTechnologyTravel

Run Your First JavaScript Program

Updated on October 30, 2011

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:

    <title>Hello World</title>

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

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."


    0 of 8192 characters used
    Post Comment

    No comments yet.


    This website uses cookies

    As a user in the EEA, your approval is needed on a few things. To provide a better website experience, uses cookies (and other similar technologies) and may collect, process, and share personal data. Please choose which areas of our service you consent to our doing so.

    For more information on managing or withdrawing consents and how we handle data, visit our Privacy Policy at:

    Show Details
    HubPages Device IDThis is used to identify particular browsers or devices when the access the service, and is used for security reasons.
    LoginThis is necessary to sign in to the HubPages Service.
    Google RecaptchaThis is used to prevent bots and spam. (Privacy Policy)
    AkismetThis is used to detect comment spam. (Privacy Policy)
    HubPages Google AnalyticsThis is used to provide data on traffic to our website, all personally identifyable data is anonymized. (Privacy Policy)
    HubPages Traffic PixelThis is used to collect data on traffic to articles and other pages on our site. Unless you are signed in to a HubPages account, all personally identifiable information is anonymized.
    Amazon Web ServicesThis is a cloud services platform that we used to host our service. (Privacy Policy)
    CloudflareThis is a cloud CDN service that we use to efficiently deliver files required for our service to operate such as javascript, cascading style sheets, images, and videos. (Privacy Policy)
    Google Hosted LibrariesJavascript software libraries such as jQuery are loaded at endpoints on the or domains, for performance and efficiency reasons. (Privacy Policy)
    Google Custom SearchThis is feature allows you to search the site. (Privacy Policy)
    Google MapsSome articles have Google Maps embedded in them. (Privacy Policy)
    Google ChartsThis is used to display charts and graphs on articles and the author center. (Privacy Policy)
    Google AdSense Host APIThis service allows you to sign up for or associate a Google AdSense account with HubPages, so that you can earn money from ads on your articles. No data is shared unless you engage with this feature. (Privacy Policy)
    Google YouTubeSome articles have YouTube videos embedded in them. (Privacy Policy)
    VimeoSome articles have Vimeo videos embedded in them. (Privacy Policy)
    PaypalThis is used for a registered author who enrolls in the HubPages Earnings program and requests to be paid via PayPal. No data is shared with Paypal unless you engage with this feature. (Privacy Policy)
    Facebook LoginYou can use this to streamline signing up for, or signing in to your Hubpages account. No data is shared with Facebook unless you engage with this feature. (Privacy Policy)
    MavenThis supports the Maven widget and search functionality. (Privacy Policy)
    Google AdSenseThis is an ad network. (Privacy Policy)
    Google DoubleClickGoogle provides ad serving technology and runs an ad network. (Privacy Policy)
    Index ExchangeThis is an ad network. (Privacy Policy)
    SovrnThis is an ad network. (Privacy Policy)
    Facebook AdsThis is an ad network. (Privacy Policy)
    Amazon Unified Ad MarketplaceThis is an ad network. (Privacy Policy)
    AppNexusThis is an ad network. (Privacy Policy)
    OpenxThis is an ad network. (Privacy Policy)
    Rubicon ProjectThis is an ad network. (Privacy Policy)
    TripleLiftThis is an ad network. (Privacy Policy)
    Say MediaWe partner with Say Media to deliver ad campaigns on our sites. (Privacy Policy)
    Remarketing PixelsWe may use remarketing pixels from advertising networks such as Google AdWords, Bing Ads, and Facebook in order to advertise the HubPages Service to people that have visited our sites.
    Conversion Tracking PixelsWe may use conversion tracking pixels from advertising networks such as Google AdWords, Bing Ads, and Facebook in order to identify when an advertisement has successfully resulted in the desired action, such as signing up for the HubPages Service or publishing an article on the HubPages Service.
    Author Google AnalyticsThis is used to provide traffic data and reports to the authors of articles on the HubPages Service. (Privacy Policy)
    ComscoreComScore is a media measurement and analytics company providing marketing data and analytics to enterprises, media and advertising agencies, and publishers. Non-consent will result in ComScore only processing obfuscated personal data. (Privacy Policy)
    Amazon Tracking PixelSome articles display amazon products as part of the Amazon Affiliate program, this pixel provides traffic statistics for those products (Privacy Policy)