A Simple JavaScript Program

By: Dusty Arlia
Published on January 29, 2013
Last Updated on Friday, July 03, 2015 at 12:13 PM
Total Updates: 4

Below you will see a basic JavaScript program:

<!doctype html>
<title>A Simple JavaScript Program</title>
<script type="text/javascript">
document.write("<h1>JavaScript Example</h1>");

The opening and closing <script> tags tell the web browser that the upcoming text is a script - in this case, it has a type of text/javascript. The browser sees the opening <script> tag and passes its contents to the browser's internal JavaScript interpreter, which then executes the JavaScript.

This JavaScript program is just one line in length:

document.write("<h1>JavaScript Example</h1>");

This is a call to the document object's write method. A call to this method places the <h1>JavaScript Example</h1> HTML into the document. This line of JavaScript is terminated with a semicolon (;) as is true for most lines of JavaScript. JavaScript executes from the top down. Each line is read, parsed, and then ran by the browser. Sometimes if there is an error at the beginning of the JavaScript program, the rest of the program will not execute.