Appendix C

JavaScript Commands and Grammar


CONTENTS

Finding information on programming in JavaScript can be a bit like looking for the Holy Grail. Between Netscape's site, online tutorials, and examples, information seems to be everywhere but at your fingertips. So here is the information you're looking for in one place, including statements, operators, and color values.

JavaScript Statements

The statements used to control program flow in JavaScript are similar to Java and C. A statement can span several lines if needed, or several statements can be placed on the same line. The key to remember is that a semicolon must be placed between all statements. Since JavaScript is not strict in its formatting, you must provide the line breaks and indentation to make sure the code is readable and easy to understand later.

break  Terminates the current for or while loop and passes control to the first statement after the loop.

comment  Notes from the script author that are ignored by the interpreter. Single line comments are preceded by //. Multiple line comments begin with /* and end with */.

continue  Passes control to the condition in a while loop and to the update expression in a for loop.

for  Creates a loop with three optional expressions enclosed in parentheses and separated by semicolons, followed by a set of statements to be executed during the loop:

for( initialExpression; condition; updateExpression) {
statements...
}

The initial expression is used to initialize the counter variable, which can be a new variable declared with var. The condition expression is evaluated on each pass through the loop. If the condition is true, the loop statements are executed. The update expression is used to increment the counter variable.

for…in  Iterates a variable for all of properties of an object:

for (variable in object) {
statements...
}

For each property, it executes the statement block.

function  Declares a JavaScript function with a name and parameters. To return a value, the function must include a return statement. A function definition cannot be nested within another function.

function name ([parameter] [...,parameter]) {
statements...
}

if…else  A conditional statement that executes the first set of statements if the condition is true, and the statements following else if false. If...else statements can be nested to any level.

if (condition) {
statements...
} [else {
statements...
}]

return  Specifies a value to be returned by a function.

return expression;

var  Declares a variable and optionally initializes it to a value. The scope of a variable is the current function or, when declared outside a function, the current document.

var variableName [=value] [..., variableName [=value]]

while  Repeats a loop while an expression is true.

while (condition) {
statements...
}

with  Establishes a default object for a set of statements. Any property references without an object are assumed to use the default object.

with (object) {
statements...
}

This statement is especially useful when applied to the Math object for a set of calculations. For example,

with (Math) {
var Value1 = cos(angle);
var Value2 = sin(angle);
}

replaces

{
var Value1 = Math.cos(angle);
var Value2 = Math.sin(angle);
}

Operator Precedence

Precedence refers to the order in which compound operations are computed. Operators on the same level have equal precedence. Calculations are computed from left to right on all binary operations beginning with the operators at the top of the list and working down.

Call, member
.
[]
()
negation/increment
++
-
!
~
-
multiply/divide
*
/
%
addition/subtraction
+
-
shift
<<
>>
>>>
relational
<
>
<=
>=
equality
==
!=
 
 
 
bitwise AND
&
 
 
 
 
bitwise XOR
^
 
 
 
 
bitwise OR
|
 
 
 
 
logical AND
&&
 
 
 
 
logical OR
||
 
 
 
 
conditional
?:
 
 
 
 
assignment
=
op=
 
 
 
comma
,
 
 
 
 

JavaScript Objects

JavaScript is an object-oriented language, and as such, includes a set of built-in objects to represent the HTML document, especially form elements. Built-in objects can be accessed by both the client and server.

String  Contains a string of characters.

Math  Provides numerical constants and mathematical functions.

Date  Stores a date in the number of milliseconds since 1/1/1970, 00:00:00, and returns a date string in the format "Thu, 11 Jan 1996 06:20:00 GMT".

Document  The foundation object created with an HTML <BODY> tag and used to write other information to the page.

Form  An object for gathering and echoing data, created by HTML <FORM> tags.

Window  The highest precedence object accessible by JavaScript relating to the current open Navigator window. New windows and frames can also be created.

Reserved Words

The following words cannot be used as user objects or variables in coding JavaScript. Not all are currently in use by JavaScript-they are reserved for future use.
Abstractforpublic
booleanfunctionreture
breakgotoshort
byteifstatic
caseimplementssuper
catchimportswitch
charinsynchroniz
constinstanceofthised
continueintthrow
defaultinterfacethrows
dolongtransient
doublenativetrue
elsenewtry
estendsnullvar
falsepackagevoid
finalprivatewhile
finallyprotectedwith
float  

Color Values

Colors can be referenced in a variety of properties in two ways: by using the string literal or a RGB hexadecimal triplet formed by combining the three color values. For example, aliceblue is represented as F0F8FF.
Color/string literalRed GreenBlue
aliceblueF0 F8FF
antiquewhiteFA EBD7
aqua00FF FF
aquamarine7F FFD4
azureF0FF FF
beigeF5F5 DC
bisqueFFE4 C4
black0000 00
blanchedalmondFF EBCD
blue0000 FF
blueviolet8A 2BE2
brownA52A 2A
burlywoodDE B887
cadetblue5F 9EA0
chartreuse7F FFA0
chocolateD2 691E
coralFF7F 50
cornflowerblue64 95ED
cornsilkFF F8DC
crimsonDC14 3C
cyan00FF FF
darkblue00 008B
darkcyan00 8B8B
darkgoldenrodB8 860B
darkgrayA9 A9A9
darkgreen00 6400
darkkhakiBD B76B
darkmagenta8B 008B
darkolivegreen55 6B2F
darkorangeFF 8C00
darkorchid99 32CC
darkred8B00 00
darksalmonE9 967A
darkseagreen8F BC8F
darkslateblue48 3D8B
darkslategray2F 4F4F
darkturquoise00 CED1
darkviolet94 00D3
deeppinkFF 1493
deepskyblue00 BFFF
dimgray6969 69
dodgerblue1E 90FF
firebrickB2 2222
floralwhiteFF FAF0
forestgreen22 8B22
fuchsiaFF00 FF
gainsboroDC DCDC
ghostwhiteF8 F8FF
goldFFD7 00
goldenrodDA A520
gray8080 80
green0080 00
greenyellowAD FF2F
honeydewF0 FFF0
hotpinkFF69 B4
indianredCD 5C5C
indigo4B00 82
ivoryFFFF F0
khakiF0E6 8C
lavenderE6 E6FA
lavenderblushFF F0F5
lawngreen7C FC00
lemonchiffonFF FACD
lightblueAD D8E6
lightcoralF0 8080
lightcyanE0 FFFF
lightgoldenrodyellowFA FAD2
lightgreen90 EE90
lightgreyD3 D3D3
lightpinkFF B6C1
lightsalmonFF A07A
lightseagreen20 B2AA
lightskyblue87 CEFA
lightslategray77 8899
lightsteelblueB0 C4DE
lightyellowFF FFE0
lime00FF 00
limegreen32 CD32
linenFAF0 E6
magentaFF00 FF
maroon8000 00
mediumaquamarine66 CDAA
mediumblue00 00CD
mediumorchidBA 55D3
mediumpurple93 70DB
mediumseagreen3C B371
mediumslateblue7B 68EE
mediumspringgreen00 FA9A
mediumturquoise48 D1CC
mediumvioletredC7 1585
midnightblue19 1970
mintcreamF5 FFFA
mistyroseFF E4E1
moccasinFF E4B5
navajowhiteFF DEAD
navy0000 80
oldlaceFDF5 E6
olive8080 00
olivedrab6B 8E23
orangeFFA5 00
orangeredFF 4500
orchidDA70 D6
palgoldenrodEE E8AA
palegreen98 FB98
paleturquoiseAF EEEE
palevioletredDB 7093
papayawhipFF EFD5
peachpuffFF DAB9
peruCD85 3F
pinkFFC0 CB
plumDDA0 DD
powderblueB0 E0E6
purple8000 80
redFF00 00
rosybrownBC 8F8F
royalblue41 69E1
saddlebrown8B 4513
salmonFA80 72
sandybrownF4 A460
seagreen2E 8B57
seashellFF F5EE
siennaA052 2D
silverC0C0 C0
skyblue87CE EB
slateblue6A 5ACD
slategray70 8090
snowFFFA FA
springgreen00 FF7F
steelblue46 82B4
tanD2B4 8C
teal0080 80
thistleD8BF D8
tomatoFF63 47
turquoise40 E0D0
violetEE82 EE
wheatF5DE B3
whiteFFFF FF
whitesmokeF5 F5F5
yellowFFFF 00
yellowgreen9A CD32