How to create your first Javascript program

Description

Save the following code into a text file and name it as test.htm. Drag and drop test.htm to a browser.


<!DOCTYPE HTML>
<html>
<head>
  <script type="text/javascript">
    document.writeln("Hello");
  </script>
</head><!-- w  w w  . j  av a 2 s .  c  o  m-->
<body>
</body>
</html>

Click to view the demo

The code above generates the following result.

The code above outputs string "Hello" to the web page.

Statements

Each statement represents a single command, and statements are usually terminated by a semicolon (;).

The following code has two statements and each outputs a string to the web page.


<!DOCTYPE HTML>
<html>
<body>
  <script type="text/javascript">
    document.writeln("This is a statement");
    document.writeln("This is also a statement");
  </script>
</body><!--  w ww  .  j  a  v  a2  s.  c om-->
</html>

Click to view the demo

The code above generates the following result.





















Home »
  Javascript »
    Javascript Introduction »




Script Element
Syntax
Data Type
Operator
Statement
Array
Primitive Wrapper Types
Function
Object-Oriented
Date
DOM
JSON
Regular Expressions