Hello, World, in JavaScript

bulletIntroduction

On this page we look at the first step necessary towards successful Java scripting, that is, how to write the Hello World program. JavaScript is a programming language designed to customize, manipulate and automate tasks related to presentation of information in an Internet browser. Learning of JavaScript requires no investment in additional software, because it is processed by the browser, and most PC operating systems arrive with an Internet browser already installed. By the same reason, Java scripts are platform independent, since they are no more but plain text files with appropriate lines of code.

bulletHello, World!

The standard JavaScript introductory hello world script is a part of an HTML code and looks like this:


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

The entire script is contained in the script element and is rather self-explanatory. In practice, the HTML document is long and difficult to read. It is thus advised to save the script in a special file, say hello.js, with the extension .js, and include the file using the src attribute. This improves not only readability, but also reusability of the script. The modified code looks like this:


<html>
   <head>
      <script type="text/javascript" src="hello.js">
      </script>
   </head>
   <body>
      ...
   </body>
</html>
		

The syntax of JavaScript is less strict than that of other languages. For example, type of variables does not have to be declared and one-line command statements should, but do not have to, end with a semicolon. Consequently, only one command statement is admissible per line, because the end of the line signals the end of the statement. Strings are an exception because they admit spreading over several lines using the line continuation operator \. One must be careful though not to leave any blank spaces after the slash, because the browser might not interpret the slash as the continuation symbol any more. E. g. to spread a long string over several lines we write:


var str="This starts a very long sentence \
             that just goes on \
               and on ...";
		

JavaScript is an object-based language with a collection of objects built-in as a part of the language's or browser's specification, a set of operators, and a syntax, resembling that of C, C++ or Java. Say, it recognizes both C comments (/* */) as well as C++ comments (//). We recall that it has five primitive types: Undefined, Null, Boolean, Number, and String. Objects include the Global object, the Object object, the Function object, the Array object, the String object, the Boolean object, the Number object, the Math object, the Date object etc. Instances of objects are created using the new operator. Behavior of variables created without new depends on the default constructor. Because a function in JavaScript is a data value, a property and a method is the same.

bulletReferences

David Barron: Scripting Languages. Wiley 2000

The ECMAScript scripting language. World Wide Web Consortium 1999