Tokens
name | description |
identifier | Name of a variable, function, or object |
keyword | A word with fixed meaning in JavaScript (i.e., cannot be used as an identifier) |
literal | A JavaScript number or string (e.g., 23, -17.5, 'boo!') |
operator | Symbols used to describe mathematical or string computations (e.g., *, +, / ) |
Types
Type | Examples |
number | -19, 3.14159, 6.02E23 |
boolean | true, false |
string | "hi!", 'The rain in spain falls mainly on the plain.' |
function | open, ... |
object | document, window |
A condensed summary (from
here
).
Comment lines inside a SCRIPT tag:
// comment rest of line
/* Begin (multiple line) comment section
*/ End (multiple line) comment section
<!-- HTML comment
Assigning values:
x = 4 A number
y = 6.758
someWord = "Jonny" A string
somePhrase = "Thank you for visiting"
mickso = true A boolean
var myVariable Defines "myVariable" with value 'undefined'
var numA = 100 Defines "numA" as number with a value of 100
Operators & Keywords:
+ Addition operator {3+4 is 7, 'ban' + 'ana' is 'banana'}
- Subtracton operator
* Multiplication operator
/ Division operator
% Modulus operator {e.g., 10%3 is 1, 9%3 is 0, 8%3 is 2)
+= a += b is the same as a = a + b
-= a -= b is the same as a = a - b
*= a *= b is the same as a = a * b
/= a /= b is the same as a = a / b
%= a %= b is the same as a = a % b
++ a++ is the same as a=a+1}
-- a-- is the same as a=a-1}
eval(string) Treat string as JavaScript code, parse it and evaluate it
Control Flow:
debugger Launch the Script Debugger and pause the script at this statement
if(num==6) if condition is true...
{ statment(s) } ...execute these statements...
else {statement} ...otherwise...
{ statment(s) } ...execute these statements (i.e., condition is false)
? : conditional operator // a = cond ? yes : no; is same as if (cond) {a=yes} else {a=no}
switch (value) { Compare value to case statements
case 1: if (value == 1)...
statement(s); ..execute these statements
break; ..
case 27: else if (value == 27)...
statement(s);
break;
default: if no case statments matched value...
statement(s); ...execute these statements
break;
}
for(n=1;n<9;n++) Loop a set number of times
{ statment(s) }
for(a in b) Loop over a set of objects
{ statment(s) }
while(count<9) Loop while condition is true (test before loop statements)
{ statment(s) }
do {statement} Loop while condition is true (test after loop statements)
while (i>0)
break Exit inner-most control structure (do, for, switch, while)
continue Skip rest of this loop iteration and evaluate the loop condition
Comparison & Logical Operators:
== equal to
!= not equal to
< less than
> greater than
<= less than or equal to
>= greater than or equal to
&& logical AND statement
|| logical OR statemant
Functions:
function foo(args) { Define a function
statment(s) Statements in function
return something; Value of function is something
}
myFunc.arguments.length Number of arguments passed to function myFunc()
myFunc.arguments[i] Array of arguments passed to myFunc (0 is first argument)
Arrays:
a = new Array() Create new array with no elements
a = new Array(n) Create new array with n elements, each of which has the value 'undefined'
a = new Array(e1,e2,...,eN) Create new array and initialize first N elements to e1, e2, ... , eN respectively
a[0] First element in array; arrays can only be indexed by non-negative integers
a.constructor == Array An array object's 'constructor' is always Array
n = a.length Returns the size of the Array a (i.e., 'a=new Array(27)' causes a.length == 27)
a.length = n Modify the length of a; if a.length>n, truncate array; if a.length<n, expand array
a.prototype Way to add "methods" to a class (see MSDN)
a3 = a1.concat(a2) Create new Array that contains all elements of Array a1 followed by all elements of Array a2
s2 = a.join(s1) Create a single string from the elements of Array a, each separated by string s1
e = a.pop() Removes the last element of Array a and returns its value
a.push(e) Increase the length of Array a by one and place the element e at this new end of the array
a.reverse() Reverse the elements of Array a in place
a.shift() Remove a[0] from the array, reducing the length of the array by 1
a.slice(start[,end]) Create new Array that is a contiguious subset of the Array a (see MSDN)
a.sort([function]) Sort elements of Array a in ascending order, or in manner defined by function
a.splice(s, nD, e1, ) Remove nD elements at index s and insert new elements (see MSDN)
a.toString([radix]) Like a.join(',') except that number base radix may be specified
a.unshift(e1, ...) Insert elements at the start of Array a
a.valueOf() Identical to a.join(',')
Strings: [NOTE: I have omitted many methods (.big(), etc.) because they are unlikely to be useful!]
s = new String Create new, empty String s
n = s.length Length of String s (i.e., s = 'foo'; s.length == 3)
ch = s.charAt(i) Return the single character at position i in String s (first character in string is at position 0)
u = s.charCodeAt(i) Return the integer Unicode for the character s.charAt(i)
s = String.fromCharCode(u1,u2,...) Create a new string from series of Unicode values u1, u2, etc.
i = s.indexOf(str[,iStart]) Search from left to right (starting at iStart), find first occurence of str in s; return -1 if not found
i = s.lastIndexOf(str[,iStart]) Search from right to left, but otherwise like s.indexOf()
a = s.match(RegExp) Use a "regular expression" to search String s [NOTE: very powerful, not well documented!]
a = s.replace(RegExp,str) Use a "regular expression" to search String s and replace matches [NOTE: not well documented!]
a = s.search(RegExp) Use a "regular expression" to search String s [NOTE: very powerful, not well documented!]
s2 = s1.slice(start[,end]) Create new String which is the substring of s1 starting at start up to but not including end
a = s.split(sep[,limit]) Create new Array of strings by spliting up String s broken up by String or RegExp sep
s2 = s1.substr(start[,end]) Almost the same as s.slice() ==> use slice()
s2 = s1.substring(start[,end]) Almost the same as s.slice() ==> use slice()
s2 = s1.toLowerCase() Return new String that is identical to s1 except all uppercase characters are now lowercase
s2 = s1.toUpperCase() Return new String that is identical to s1 except all lower case characters are now uppercase
More Operators & Keywords:
n = parseFloat(string) Convert string to a floating point number
n = parseInt(string) Convert string to an integer
t = typeof(variable) Returns type of variable: "number", "string", "boolean", "function", "object", "undefined"
Math Functions: // e.g., integer = Math.floor(floating_point)
Math.abs(num) Returns the absolute value of a number
Math.acos(num) Returns the inverse cos of a number
Math.asin(num) Returns the inverse sin of a number
Math.atan(num) Returns the inverse tan of a number
Math.atan2(x,y) Returns some oddball tan thing
Math.ceil(num) Rounds up a number
Math.cos(angle) Returns the cos of an angle
Math.exp(num) Returns the exponent of num
Math.floor(num) Rounds down a number
Math.log(num) Returns the log of a number
Math.max(num1,num2) Compare two numbers and returns the largest
Math.min(num1,num2) Compares two numbers and returns the smallest
Math.pow(num,p) Raises num to the power p
Math.random() Generate a random number between 0.0 and 1.0
Math.round(num) Rounds off a number
Math.sin(angle) Returns the sin of an angle
Math.sqrt(num) Returns the square root of a number
Math.tan(angle) Returns the tan of an angle
Math.toString(num) Convert num to a string
Math.valueOf(object) Returns the numeric value of an object
Date & Time:
new Date() Returns the current date from the system clock
getYear() Returns the year from the date found
getMonth() Returns the month
getDay() Returns the day (numeric - Sunday = 0)
getDate() Returns the date
getHours() Returns the hours
getMinutes() Returns the minutes
getSeconds() Returns the seconds
HTML Event keywords: // <span onClick="handleClickEvent()">some stuff to click on</span>.
onClick="doFunc()" Call doFunc() when user clicks mouse over this HTML region
onFocus="doFunc()" Call doFunc() when this HTML region gains focus (of keystrokes)
onBlur="doFunc()" Call doFunc() when this HTML region loses focus (of keystrokes
onChange="doFunc()" Call doFunc() when the contents of this HTML region changes (esp for <input type="text" size="20">)
onMouseOver="doFunc()" Call doFunc() when user moves mouse into this HTML region
onMouseOut="doFunc()" Call doFunc() when user moves mouse out of this HTML region
Dialog boxes:
alert(message) message plus "OK" option
confirm(message) message plus "OK" & "Cancel" options
prompt(message,default) message plus dialog input box plus "OK" & "Cancel" options
Browser Objects:
self refers to browser window
self.status contents of status line
window refers to object in browser
defaultStatus status line default message
myNewWin where myNewWin is the name given to a
second browser opened
myForm where myForm is name given to a form
myFrame where myframe is name of one frame used
document current browser contents
bgColor background colour
fgColor foreground colour (text)
linkColor an unvisited link
alinkColor an active link (currently being clicked)
vlinkColor a visited link
location pages source location
reload() ???????
|