Hello, World!

JavaScript is an interpreted language. That means: no compilation. You just write code and attach it to HTML.

The simplest way is to put JavaScript directives into SCRIPT tag somewhere inside the page.

Here is a SCRIPT tag containing single JavaScript statement. Please click the ‘Show’ button to see the example.

<!DOCTYPE HTML>
<html>
<body>

  <p>Header...</p>

  <script>
    alert('Hello, World!')
  </script>

  <p>...Footer</p>

</body>
</html>

Note that contents of the SCRIPT block is not shown, instead, it is executed.

This example makes use of following elements:

<script> ... </script>
<script> tag tells browser that there is executable content inside. In older HTML standard, namely HTML4, a special attribute type was required. Although more up-to-date HTML5 makes it optional.

Browser:

  1. Starts showing the page, shows Header
  2. Switches to JavaScript mode inside this tag and executes the contents of <script>
  3. Returns back to HTML-mode and continues with the page, outputs Footer.
alert(...)
Outputs a message window on the screen and awaits until visitor presses OK

Not only reading, but actually testing and doing something on your own is very important. Below you can see a “check yourself” block. It contains a task and the solution.

Fulfil the task to ensure you got eveything right.

Create a page which outputs a “I am JavaScript!”. Save the document and open in the browser. Make sure everything works.

Or, in other words, create the page which works like: tutorial/browser/script/alert/index.html.

Open solution
Solution

Open source code of the given example to see the solution or just here: tutorial/browser/script/alert/index.html.

On next pages we’ll go deeper into the examples and basic script building blocks.

Tutorial

Donate

Donate to this project