JavaScript: Introduction, Purpose and Comments

Recommended Knowledge

Basic HTML knowledge is recommended. Click here to go to the HTML collection.

JavaScript is a so-called client-side programming language, which means that JavaScript programs are processed directly in the browser. This is possible, because JavaScript is embedded in HTML. Therefore, HTML-files can contain JavaScript-Code.

First of all, we'll show you where and how you can include JavaScript-Code.

  • Between <script> and </script>
  • In an external file
  • In form of an HTML-link
  • As a parameter of an HTML-tag

We use the following command as an example:

Our example will display the text: "hello js". You'll see later how this command works exactly. The most obvious method to use JavaScript commands is to use the <script>-tag.

In JavaScript, commands are displayed under each other, each in one line. If you want multiple commands in one line, you have to separate them with semicolons. Unlike other programming languages like Java, not every command must end with a semicolon. It's recommendable to separate all commands with semicolons, because it keeps your code more flexible and cleaner. The following examples are equivalent.

JavaScript code is interpreted by the browsers JavaScript interpreter. For example, consider the following HTML file:

After being interpreted by a browser, this code is equivalent to the following code:


Easy examples

Of course we don't demonstrate the broad range of functions in JavaScript immediately.


If you run this or any of the other examples in Microsoft Internet Explorer (IE9-), you'll get a warning message. So you have to click a few times, until the code is executed. There are two antidotes:

  • You can install a local server and access the examples through the web server.
  • You can activate the checkbox at   Menu Tools => Internet Options => Advanced => Security => "Allow active content to run in files on my computer"

The type-attribute

In XHTML or HTML 4 the type-attribute was required. This is a newer alternative to language. Here's an example:

In HTML 5 this attribute isn't important anymore. It's your decision whether you use it or not. For older browsers, the type-attribute is recommendable.

Browsers without JavaScript

Even if a JavaScript program can run pretty good on one computer, it is important that it runs for as many visitors of the website as possible. In some corporate networks it's forbidden to activate JavaScript for security reasons. The previous examples then would look like this in a browser:

Empty Browser

Comments in JavaScript

If we need to take notes in the code, to describe singe parts of the code or explain how it works, we can use comments. There are two types of comments in JavaScript:

  • //

    Displays a single-line comment. Everything that is in the respective line is ignored by the JavaScript interpreter.
  • /* and */

    Displays a multiline comment. Everything between / * and * / is ignored by the JavaScript interpreter.

The following example shows you how to use JavaScript comments.


document.write("This is a sentence");
//"This is a sentence" is displayed in a browser

/*
Me and
my colegue are
JavaScript comments -
only I can be in multiple lines. Muhaha!
*/



Continue to part 2




Comment