appendix B
JavaScript Statements, Functions,
Operators, and Keywords Reference
CONTENTS
This appendix is a list of the statements and functions available
in JavaScript and their uses. It also includes a complete list
of the keywords that are reserved by JavaScript.
This section contains an alphabetical listing of the statements
available in JavaScript and their syntax.
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.
This statement is used to break out of the current for
or while loop. Control resumes
after the loop, as if it had finished.
This statement continues a for
or while loop without executing
the rest of the loop. Control resumes at the next iteration of
the loop.
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 }
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 }
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;
}
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");
Conditional statements are explained further in Chapter 3 "Working
with Objects and Events."
This statement ends a function and optionally returns a value.
The return statement is necessary
only if a value is returned.
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.
See the section titled The Array Object in Chapter 4 "Using
Built-In Objects and Custom Objects," or appendix A, "JavaScript
Structure and Objects Reference," for details.
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 }
The functions in the next sections are built into JavaScript,
rather than being methods of a particular object.
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.
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).
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.
This function returns true
if a value is not a number ("NaN").
This function works on UNIX platforms only.
This function converts a string to URL-encoded (escaped) form.
All nonalphanumeric characters are converted to %
and their ASCII value to hexadecimal.
This function converts an escaped (URL-encoded) string to normal
text. It can be used to convert characters in URLs.
This function taints (marks) a variable or property with the current
script's taint code. Data tainting is explained in Chapter 10,
"Working with Multiple Pages and Data."
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 includes a variety of operators that can be used in
expressions. The following operators are used for assignment:
- = uses the variable on
the left to store the result of the expression on the right.
- += adds the number on
the right to the variable on the left.
- -= subtracts the number
on the right from the variable on the left.
- *= multiplies the variable
by the number on the right.
- /= divides the variable
by the number on the right.
- %= uses the modulo operator,
described in the next section.
The following operators are used for mathematical expressions:
- + adds two numbers.
- - subtracts one number
from another.
- * multiplies two numbers.
- / divides one number
by another.
- % (modulo) is the remainder
when two numbers are divided.
- - (unary minus) changes
a number to its negative version (complement).
- ++ adds 1 to (increments)
a variable. This can be used as either a prefix or a postfix.
- - subtracts 1 from (decrements)
a variable. This can be used as either a prefix or a postfix.
A single operator works with string values: +
concatenates (combines) two string values.
The following operators are used for conditions and comparisons:
- Equal (==)
- Not equal (!=)
- Less than (<)
- Greater than (>)
- Greater than or equal to (>=)
- Less than or equal to (<=)
The following operators are used for logical expressions using
Boolean values:
These operators are used for binary and bitwise operations:
- And (&) returns one
if both of the corresponding bits are one.
- Or (|) returns one if
either of the corresponding bits is one.
- Xor (Exclusive Or) (^)
returns one if either, but not both, of the corresponding bits
is one.
- Left shift (<<)
shifts the bits in the left operand a number of positions specified
in the right operand.
- Right shift (>>)
shifts to the right, including the bit used to store the sign.
- Zero-fill right shift (>>>)
fills to the right, filling in zeros on the left.
Finally, the following operators are used for working with variables
and functions:
- typeof returns the type
of a variable or literal. This is a string consisting of the values
number, string,
boolean, function,
object, or undefined.
- void() can be used with
a function definition to force it to evaluate to undefined
instead of returning a value.
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
| |
|