JavaScript Summary

counter = 0; ....; counter = counter+1; etc.
var step, n; // Declaring variables is optional, but it is a good idea, important in functions.
Integers:7, -7 etc.
Octal:012= 1010
Hex:0xff = 25510, useful for rgb colours
Floating Point:314.0e-2 == 3.14
"a string" and 'another string'
NB. HTML uses " for strings, so in those places that HTML and JavaScript meet, JavaScript should use '.
\n newline, \t tab, \' quote, \" double quotes
Arrays, sparse
new Array( )creates a new one
var a = new Array( ) declares an array variable
a[7]7th element
a["fred"]the fred element, equivalent to a.fred
new someObject( ) create a new one; can omit ( ) if no params
var o = new someObject
o.faccess field etc.
function fnName(param1, param2) { ... code ... }
return, return value, optionally can "drop out" the end of a fn
The JavaScript operators are drawn from C.
NB. + is numeric addition and also string concatenation
Being an interpreted "scripting" language, operands are converted to appropriate types for an operator if possible. This raises the question of whether "2"+"3" equals 5 or "23"? What do you think?
Equality == Note that strings are compared by value, not reference, c.f. C
typeof expression
returns "number", "string", "boolean", "object", "function" or "undefined".
aVariable = anExpression;
total += 2; // as in C
if(expression) statement
if(expression) statement else statement
for(start; test; change) statement
for(variable in anObject) statement // runs through anObject's fields
while(expression) statement
break // quit a loop
continue // next iteration of loop
Compound Statement
{ statement0 statement1 ... }
with( anObject ) statement // anObject's fields become visible, c.f. Pascal
re Functions
return expression; // a result
User Defined Objects
function MyObject(p1, p2, ...)        // constructor
 { this.f1 = p1; this.f2 = p2; ... }

MyObject.prototype.f3 = someExpression;
MyObject.prototype.f4 = someFunction; // etc.
// need `prototype' keyword as we are defining a class not an object instance

System Objects - some predefined JavaScript objects are listed here:
Array, e.g. new Array( ), var a = new Array(10), a.length==10, a.reverse( ), a.sort( ), arrays can be sparse and can "grow".
Boolean, wrapper class for boolean values.
Date, new Date( ), d.milliseconds (since 1/1/1970), d.getDate( ) (1..31), d.getSeconds( ), d.getMinutes( ), d.getHours( ), d.getDay( ) (0..6), d.getMonth( ) (0..11), d.getYear( ), and also d.setDate( ), ... etc.
Document // the HTML page in the Window
NB. document == window.document
document.applets[ ], document.applets.length
document.images[ ], document.images.length
... etc.
Button, b.form, b.name, b.value, b.type (=="button"), b.onclick( ) event handler.
Checkbox, ch.form, ch.name, ch.value, ch.type, ch.checked, ch.defaultChecked :boolean
Frame, the sub-window objects of a window, see Window.
Function // a JavaScript function
History // the history of visited pages
Image // image in an HTML page
objects relevant to Java:
JavaClass // a Java Class for communication with applets
JavaObject // esp' applets
Location // an URL, as in Window.location
Math // mathematical routines and constants
Math.E, Math.PI, ...
Math.cos( ), Math.acos( ), ...
Math.random( ) (result in [0.0 ... 1.0]), ...
Navigator // the browser; it must annoy m.s.!
Object // the empty object
Window // a frame or window of the browser
window.self, window.parent, window.top // the window/frame tree
window.frames[ ], window.frames.length
window.setTimeout(javascript-code, msecs-delay)
window.onload( ) window.onunload( ) // attributes set in HTML <BODY...> tag or <FRAMESET..> tag.
Many attributes of JavaScript objects come under the protection of the security model. It is important that a JavaScript program in an HTML page and originating from a certain web site should not be able to discover all information about the user, the pages he or she has visited, data typed into forms etc. Such data are tainted and values computed from them are also tainted to prevent them from being misused, e.g. for "marketing" purposes.

Copyright © L.Allison Department of Computer Science, Monash University, Australia 3168 / 1997