JavaScript Reference


CONTENTS

JavaScript Statements

This section contains an alphabetical listing of the statements available in JavaScript and their syntax.

Comments

Comments are used to include a note within a JavaScript program and are ignored by the interpreter. There are two different types of comment syntax:

//this is a comment
/* this is also a comment */

Only the second syntax can be used for multiple-line comments; the first must be repeated on each line.

break

This statement is used to break out of the current for or while loop. Control resumes after the loop, as if it had finished.

continue

This statement continues a for or while loop without executing the rest of the loop. Control resumes at the next iteration of the loop.

for

This statement defines a loop, usually to count from one number to another using an index variable. In this example, the variable i counts from 1 to 9:

for (i=1;i<10;i++;) { statements }

for...in

This is a different type of loop, used to iterate through the properties of an object or the elements of an array. This statement loops through the properties of the Scores object, using the variable x to hold each property in turn:

for (x in Scores) { statements }

function

This statement defines a JavaScript function that can be used anywhere within the current document. Functions can optionally return a value with the return statement. This example defines a function to add two numbers and return the result:

function add(n1,n2) {
   result = n1 + n2;
   return result;
}

if...else

This is a conditional statement. If the condition is true, the statements after the if statement are executed; otherwise, the statements after the else statement (if present) are executed. This example prints a message stating whether a number is less than or greater than 10:

if (a > 10) {
   document.write("Greater than 10");
}
else {
   document.write("10 or less");
}

A shorthand method can also be used for these types of statements, where ? indicates the if portion and: indicates the else portion. This statement is equivalent to the previous example:

document.write((a > 10) ? "Greater than 10": "10 or less");

return

This statement ends a function and optionally returns a value. The return statement is necessary only if a value is returned.

var

This statement is used to declare a variable. If you use it within a function, the variable is guaranteed to be local to that function. If you use it outside the function, the variable is considered global. Here's an example:

var students = 30;

Because JavaScript is a loosely typed language, you do not need to specify the type when you declare the variable. A variable is also automatically declared the first time you assign it a value:

students = 30;

Using var will help avoid conflicts between local and global variables. Note that arrays are not considered ordinary JavaScript variables; they are objects.

while

The while statement defines a loop that iterates as long as a condition remains true. This example waits until the value of a text field is "go":

while (document.form1.text1.value != "go") {statements }

JavaScript Built-In Functions

The functions in the next sections are built into JavaScript, rather than being methods of a particular object.

eval

This function evaluates a string as a JavaScript statement or expression, and either executes it or returns the resulting value. In the example below, a function is called using variables as an argument:

a = eval("add(x,y);");

eval is typically used to evaluate an expression or statement entered by the user.

parseInt

This function finds an integer value at the beginning of a string and returns it. If there is no number at the beginning of the string, Windows platforms return 0; other platforms return "NaN" (not a number).

parseFloat

This function finds a floating-point value at the beginning of a string and returns it. If there is no number at the beginning of the string, either 0 or "NaN" (not a number) is returned.

isNaN()

This function returns true if a value is not a number ("NaN"). This function works on UNIX platforms only.

escape()

This function converts a string to URL-encoded (escaped) form. All nonalphanumeric characters are converted to % and their ASCII value to hexadecimal.

unescape()

This function converts an escaped (URL-encoded) string to normal text. It can be used to convert characters in URLs.

taint()

This function taints (marks) a variable or property with the current script's taint code. 

untaint()

This function removes taint from (unmarks) a variable or property. This only works if the value carries the current script's taint code. If the value came from another script or another server, it cannot be untainted.

JavaScript Operators

JavaScript includes a variety of operators that can be used in expressions. The following operators are used for assignment:

The following operators are used for mathematical expressions:

A single operator works with string values: + concatenates (combines) two string values.

The following operators are used for conditions and comparisons:

The following operators are used for logical expressions using Boolean values:

These operators are used for binary and bitwise operations:

Finally, the following operators are used for working with variables and functions:

JavaScript Keywords

This is a list of all the keywords, or reserved words, in the JavaScript language. These may be statements, functions, or connecting words. Some of the words in this list are not currently used in JavaScript, but have been listed as reserved words by Netscape because they may be used in a future version.

The main reason for this list is to remind you of which words you cannot use as variable, function, or object names. Using them may result in unpredictable behavior.

  • abstract
  • int
  • boolean
  • interface
  • break
  • long
  • byte
  • native
  • case
  • new
  • catch
  • null
  • char
  • package
  • class
  • private
  • const
  • protected
  • continue
  • public
  • default
  • return
  • do
  • short
  • double
  • static
  • else
  • super
  • extends
  • switch
  • false
  • synchronized
  • final
  • this
  • finally
  • throw
  • float
  • throws
  • for
  • transient
  • function
  • true
  • goto
  • try
  • if
  • var
  • implements
  • void
  • import
  • while
  • in
  • with
  • instanceof

  • Built-In Objects

    Array

    You can create a new Array object to define an array-a numbered list of variables. (Unlike other variables, arrays must be declared.) Use the new keyword to define an array, as in this example:

    students = new Array(30)
    

    Items in the array are indexed beginning with 0. Refer to items in the array with brackets:

    fifth = students[4];
    

    Arrays have a single property, length, which gives the current number of elements in the array. They have the following methods:

    String

    Any string of characters in JavaScript is a string object. The following statement assigns a variable to a string value:

    text = "This is a test."
    

    Because strings are objects, you can also create a new string with the new keyword:

    text = new String("This is a test.");
    

    string objects have a single property, length, which reflects the current length of the string. There are a variety of methods available to work with strings:

    There are also a few methods that enable you to change a string's appearance when it appears in an HTML document:

    As an example, this statement prints the value of the text string in italics:

    document.write(text.italics());
    

    Math

    The Math object is not a "real" object, because you can't create your own objects. Each property or method uses the built-in Math object. A variety of mathematical constants are available as properties of the Math object:

    The methods of the Math object enable you to perform mathematical functions. The methods are listed in the following sections in categories.

    Algebraic Functions

    Statistical and Logarithmic Functions

    For example, this statement assigns the big variable to the larger of x and y:

    big = Math.max(x,y);
    

    Basic Math and Rounding

    As an example, the following statement assigns the x variable to the square root
    of 35:

    x = Math.sqrt(25);
    

    Random Numbers

    Math.random() returns a random number between 0 and 1.

    Note
    The Math.random() method worked only on UNIX platforms until Netscape 3.0. Be sure you and your users use the latest version.

     

    Date

    The Date object is a built-in JavaScript object that enables you to work conveniently with dates and times. You can create a Date object any time you need to store a date and use the Date object's methods to work with the date:

    Note
    The Date object will not work with dates before January 1st, 1970.

     

    navigator

    The navigator object includes information about the current browser version. At present, it works only with Netscape browsers. Its properties include the following:

    The JavaScript Object Hierarchy

    The object hierarchy includes objects that represent the browser window, the current document, and its contents.

    window

    The window object represents the current browser window. If multiple windows are open or frames are used, there may be more than one window object. These are given aliases to distinguish them:

    Each window object includes the following properties:

    The window object also has three child objects, which you'll look at in their own sections later:

    The window object includes the following methods:

    Finally, window objects have the following event handlers, which you can define in the document's <BODY> or <FRAMESET> tag:

    location

    The location object contains information about the current URL being displayed by the window. It has a set of properties to hold the different components of the URL:

    The location object also has two methods:

    history

    The history object holds information about the URLs that have been visited before and after the current one in the window, and it includes methods to go to previous or next locations:

    document

    The document object represents the current document in the window and is a child of the window object. It includes the following properties:

    The document object also includes the following child objects as properties:

    The document object has no event handlers. It includes the following methods:

    Creating and Customizing Objects

    This is a brief summary of the keywords you can use to create your own objects and customize existing objects. 

    Creating Objects

    There are three JavaScript keywords used to create and refer to objects:

    To create a new object, you need an object constructor function. This simply assigns values to the object's properties using this:

    function Name(first,last) {
       this.first = first;
       this.last = last;
    }
    

    You can then create a new object using new:

    Fred = new Name("Fred","Smith");
    

    You can also create a generic object using the Object() constructor and define its properties later:

    values = new Object();
    

    Customizing Objects

    You can add additional properties to an object you have created just by assigning them:

    Fred.middle = "Clarence";
    

    Properties you add this way apply only to that instance of the object, not to all objects of the type. A more permanent approach is to use the prototype keyword, which adds a property to an object's prototype (definition). This means that any future object of the type will include this property. You can include a default value:

    Name.prototype.title = "Citizen";
    

    You can use this technique to add properties to the definitions of built-in objects as well. For example, this statement adds a property called num to all existing and future string objects, with a default value of 10:

    string.prototype.num = 10;