Computer Science 15-237
Class Notes: Getting Started with Javascript
Note: Grayed-out portions are not part of this week's core material (so, do not use those techniques on hw1, and they will not appear on quiz1).
Type | Examples / Comments |
number | (typeof 42) == (typeof 3.14) // "number" |
boolean | (typeof true) == (typeof false) // "boolean", lowercase |
string | 'use single quotes' + "or double quotes" |
function | function f() { } alert(typeof f); // "function" |
object | alert(typeof document); // "object" alert(typeof [2,3]); // an array is an "object" var x = null; alert(typeof x); // null is also an "object" |
undefined | var x; alert(typeof x); // "undefined" |
Category | Operators |
Arithmetic | +, -, *, /, %, ++ (pre/post), -- (pre/post), - (unary), + (unary) |
Relational | ==, !=, === (strict equal / same type), !==, <. <=, >=, > |
Bitwise | <<, >>, >>>, &, |, ^, ~ |
Assignment | +=, -=, *=, /=, %=, <<=, >>=, >>>=, &=, |=, ^= |
Logical | &&, ||, ! |
Conditional | ?: (condition ? trueValue : falseValue) |
Comma | , (A,B) evaluates A and B, returns result of B |
Special | delete, in, instanceof, new, this, typeof, void (Deferred until we cover objects...) |
alert(0.1 + 0.1 + 0.1); // 0.30000000000000004
Type | Examples / Comments |
block | { s1; s2; s3; } |
conditional | if (condition) { trueBlock; } else { falseBlock; } |
switch | // As in C or Java switch (expression) { case labell: statements1; case label2: statements2; default: statementsN; } |
for loop | for (initializer ; condition ; incrementer) statement for (var x = 3; x < 6; x++) { document.write(x); document.write("<br>"); // newline } document.write("Finally, x =", x); |
while loop | As in most languages |
do...while loop | do statement while (condition); As in some languages. Same as while loop, but tests at the end of each pass. |
break/continue | As in most languages |
for...in for each ... in |
(Deferred until we cover objects...) |
throw | As in most languages (but you can throw most anything) |
try ... catch | try { alert("nothing wrong yet!"); alert(x); // x is undefined! alert("this will never happen!"); } catch (e) { alert("caught: " + e); } finally { alert("do stuff like close files here"); } |
Function | Examples / Comments |
eval(expr) | Evaluate the expr in the current scope |
isFinite(number) | true except for NaN, +Infinity, -Infinity |
isNaN(value) | false except for NaN |
encodeURI(s) decodeURI(s) |
Make a string safe for including in a URI |
Property | Examples / Comments |
MAX_VALUE | The largest number. About 1.8e+308. |
MIN_VALUE | The smallest number. About 5e-324. |
NaN | Special "not a number" value |
NEGATIVE_INFINITY | Special negative infinite value; returned on overflow |
POSITIVE_INFINITY | Special positive infinite value; returned on overflow |
Property | Examples / Comments |
PI | About 3.14 |
E | About 2.718 |
Method | Examples / Comments |
abs | Absolute value. |
acos | Inverse trig. Arccosine in radians. |
asin | Inverse trig. Arcsine in radians. |
atan | Inverse trig. Arctangent in radians. |
atan2 | Like atan, but takes a point to preserve quadrant. |
ceil | smallest integer n >= arg |
cos | cosine of angle in radians. |
exp | exp(b) returns eb |
floor | largest integer n <= arg |
log | natural log (base e) |
max | maximum of two args |
min | minimum of two args |
pow | pow(a,b) returns ab |
random | random float between 0 and 1. |
round | nearest integer |
sin | sine of angle in radians. |
sqrt | square root of arg. |
tan | tangent of angle in radians. |
Method | Examples / Comments |
charAt | s.charAt(i) returns char at index i |
charCodeAt | s.charCodeAt(i) returns ascii of char at index i |
indexOf | finds index of first occurrence of substring |
lastIndexOf | finds index of last occurrence of substring |
concat | creates new string concatenating the two |
split | split string into array based on delimiter |
slice | a.slice(i,j) returns substring from index i to index j, where j can be negative (from the end) or omitted. |
substring (substr) | similar to slice (see documentation for details) |
match, replace, search | Deferred until we cover regular expressions. |
toLowerCase | returns lowercase string |
toUpperCase | returns uppercase string |
Method | Examples / Comments |
concat | returns a copy of the joined arrays |
indexOf | finds index of first occurrence of element |
join | joins all the elements of the array into a string |
lastIndexOf | finds index of last occurrence of element |
pop | removes and returns the last element of the array |
push | adds new element to end of array and returns new length |
reverse | reverses the order of the array |
shift | removes and returns the first element of the array |
slice | returns a new array that is a copy of part of the array |
sort | sorts the elements in the array |
splice | adds/removes elements from the array |
unshift | adds new elements to the front of the array and returns new length |
carpe diem - carpe diem - carpe diem - carpe diem - carpe diem - carpe diem - carpe diem - carpe diem - carpe diem