An Abbreviated Introduction to JavaScript


More Simple JavaScript

Abstract: Further information to help those getting started with JavaScript.


Variables

As with many programming languages, JavaScript allows you to define and use variables -- named containers of information. Unlike many languages, JavaScript supports only untyped variables. You can use the same variable to store some text, a number, or an object.

You assign values to variables by writing the variable name, an equals sign, and the expression you want to assign to the variable. For example, alpha=2 assigns the value 2 to the variable alpha.

You declare a new variable with var varname If the declaration is within a function, the variable is local to that function. If the declaration is outside of a function, the variable is global (accessible from any function).

Surprisingly, JavaScript does not require you to declare variables. However, I strongly encourage you to declare your variables and add a note as to how you're using them.

Comments

As you develop longer and longer programs, you should insert notes to yourself (or to others) to help explain the program. These are typically called comments. In JavaScript, comments begin with two slashes, //, and end at the end of a line.

Expressions

JavaScript permits normal mathematical expressions, such as 2*10+3. It also permits you to use variables within expressions.

JavaScript also provides some basic string operations. In particular, + is used to concatenate strings. If you happened to have the user's name stored in the variable userName, you could create a greeting with

var greeting // A greeting to the user
greeting = "Hello " + userName + ".  Welcome to this page."

Inserting text and HTML

Up to this point, we've only been interacting with the user by putting up dialogue boxes or inserting information in fields. Clearly, we'd like to do more. For example, we might want to insert text or HTML into a document. JavaScript allows us to do this with the write method of the document object. That is, you use document.write(text) to insert text. The text is inserted at the point that the JavaScript code is executed. You use document.writeln(text) to write the text an add a carriage return tot he end of the line.

For example, we might create a page that greets a user by name with

<script language="javascript">
var userName
userName = prompt("Please enter your name", "")
document.write("

Hello " + userName + " welcome to my page.

") </script>

Note that I've included spaces in the strings so that the user's name does not run into the surrounding text. Also note that I've included HTML tags within the text I've written.

There is a drawback to the document.write() method: it only works when the document is loaded (or reloaded). Hence, you cannot later insert text into your document (unless you reload it).

You can play with a sample page that includes this function.


Source text written by Samuel A. Rebelsky.

This page may be found at http://www.math.grin.edu/~rebelsky/Tutorials/JavaScript/Spring1999/more.html

Source text last modified Mon Apr 12 10:52:04 1999.

This page generated on Mon Apr 12 11:00:01 1999 by SiteWeaver. Validate this page's HTML.