Sample JavaScript Program

Sample JavaScript Program - Source Code

Here's our standard sample count-to-ten program in JavaScript:

//////////////////////////////////////////////////
//
// Sample JavaScript Program - Counts to Ten
//
// Copyright 2005 Andrew Eichstaedt
// Eichstaedt Development Group
// http://www.andrew-eichstaedt.com
//
function sample_javascript_program()
{
    var message = ""
    var i

    message += "Hello! I am a sample javascript\r\n"
    message += "program that counts to ten.\r\n\r\n"

    for (i = 1; i <= 10; i++)
    message += i.toString() + "\r\n"

    message += "\r\nThanks for running me."

    alert(message)
}

General Notes on JavaScript

JavaScript is an object-oriented scripting language used most frequently for programming client-side web applications.

While JavaScript's syntax is similar to that of Java, the two languages are quite different. JavaScript, developed at Netscape, was originally named Mocha, then LiveScript, but was renamed JavaScript for marketing purposes. JavaScript and Java programs are not interchangeable. Semantics of JavaScript and Java differ significantly, and each language's object model is different and incompatible.

Unlike Java, JavaScript is actually a prototype-based programming language, without classes explicitly present, and with inheritance implemented by cloning existing objects which serve as prototypes for new objects.

JavaScript was initially devloped by Brendan Eich of Netscape in 1995. Brendan Eich later went on to become a founding member and chief architect of Mozilla, a standards-based, open source web browser program that supports JavaScript.

In response to Netscape's development of JavaScript, Microsoft developed an similar though not identical implementation of the language called JScript. The need for common specifications for both JavaScript and JScript drove the effort for specification of the ECMA 262 standard for ECMAScript.

More JavaScript programming resources are available below.

Sample JavaScript Program - Comments for Programmers

JavaScript is an interpreted language, with the interpreter generally a part of the web browser. The "Run The Script" button above is contained within an HTML form. Pressing the button invokes the sample_javascript_program function defined in the script.

The first lines of the program are comments. Two forward slashes begin a comment that continues until the end of the line. The C-style /* ... */ syntax may also be used for comments.

The line function sample_javascript_program() begins the definition of the single function in the sample program.

The var lines declare two variables: message, which we will fill with the hello message and the count to ten; and i, which will be the index variable in the for loop below.

The lines beginning with message += add the following expression to the end of the message variable. The \r\n strings use C-style syntax that signifies a carriage return followed by a newline character.

The for loop after this also uses syntax like C. As in C, i is initialized to 1, the loop runs while the condition in the middle, in this case i <= 10, is true, and with each iteration of the loop, the final expression is executed---here it is i++, which increments i.

Note that the line

message += i.toString() + "\r\n"

could also have been written

message += i + "\r\n"

because, as a loosely typed language, the conversion of numerically-valued i to a string would be done automatically.

The line alert(message) displays a dialog box showing the contents of the message variable:

Sample JavaScript Program Output

You will likely find JavaScript programs with semicolons at the end of lines. This is acceptable, but not strictly necessary. Semicolons can be used to separate multiple statements in one line, like:

alert("First"); alert("Second")

Sample JavaScript Programs - Examples from the Eichstaedt Development Group Site

This site uses JavaScript to program effects like the button rollovers and the floating navigation area on the left. View the source code for this page to see the examples.

The image rollover feature is an example of the kind of code that Macromedia Dreamweaver can automatically insert into HTML pages without the designer needing to know how to write a script.

The floating menu is implemented with a sophisticated set of routines written by Aaron Boorman.

JavaScript Programming Resources

JavaScript programming resources are available across the web. Tutorials are available at sites like http://www.w3schools.com/js/default.asp, http://www.pageresource.com/jscript/, and http://javascriptkit.com/.

For general resources, try http://www.javascript.com/ or http://javascript.internet.com/. A great quick reference is available at http://javascript-reference.info/.

     
- -