You are on page 1of 2

JavaScript Quick Reference Card

1.03
Copyright, 2007200! "rand#$atch %%C
http&''(((.e)p*ainth.at
Co*or key over*eaf
Code Structure
var ...
//Global variable declarations
function funcA([para+1,para+2,...])
,
var ...
//Local variable declarations visible in nested
functions
[function innerFuncA-[ipara+1,ipara+2...].
,
var ...
//Variables local to innerFuncA
//your code here
/]
aName012)p*ain3hat415
//implicit global variable creation
//your code here
/
Nomenclature Rules
6unction and varia7*e na+e# can con#i#t of any
a*phanu+eric character. 8 and 9 are a**o(ed. 3he fir#t
character cannot 7e nu+eric. :any e)tended ;SC<<
character# are a**o(ed. 3here i# no practica* *i+it on
na+e *ength. =a+e# are ca#e#en#itive.
<f t(o or +ore varia7*e# or function# or a varia7*e > a
function are dec*ared (ith the #a+e na+e the *a#t
dec*aration o7*iterate# a** previou# one#. ?#ing a
key(ord a# a varia7*e or function na+e o7*iterate# that
key(ord.
Visibility & Scope
;##ign+ent# (ithout the u#e of the var key(ord re#u*t
in a ne( g*o7a* varia7*e of that na+e 7eing created.
@aria7*e# dec*ared (ith the var key(ord out(ith the
7ody of a function are g*o7a*. @aria7*e# dec*ared (ith
the var key(ord in#ide the 7ody of a function are *oca*
to that function. %oca* varia7*e# are vi#i7*e to a** ne#ted
function#.
%oca* entitie# hide g*o7a*# 7earing the #a+e na+e.
Variable ypes
strin!" var s # 1e)p*ainthat1 or Ae)p*ainthatB
number" var n # 3.1C1D!, 100, 0...
boolean" var fla! 0 fa*#e or true
ob$ect" var d 0 ne( Eate-.5
function" var %reet # function #ayFe**o-. ,a*ert-1Fe**o1./
JavaScript i# a (eak*y typed *anguage G i.e. a #i+p*e
a##ign+ent i# #ufficient to change the varia7*e type. 3he
typeof key(ord can 7e u#ed to check the current
varia7*e type.
Special Values
3he #pecia* va*ue# false& 'nfinity& NaN& null& true >
undefined are recogniHed. null i# an o7Iect. 'nfinity
and NaN are nu+7er#.
(perators
(perator )*ample Result
J 3 J 2
1e)p*ain1 J 1that1
D
e)p*ainthat
+ 3 2 1
K 3K2 L
' 3'2 1.D
M 3M2 1
JJ i 0 25
iJJ
,
, JJi
-
3
i 0 25
i
,
, i
-
1
00
00
3 0 131
2 00 3
true
fa*#e
000 3 000 3
3 000 131
true
fa*#e
N 2 N 3
1a1 N 1;1
true
fa*#e
N0 2 N0 3 true
O 2 O 3 fa*#e
O0 2 O 3 fa*#e
0 i 0 2 i i# a##igned
the va*ue 2
J0 iJ01 3
0 i01 2
iK0 iK03 L
'0 i'02 3
M0 iM02 1
i 0 25I 0 D5
>> -;=E. -i N0 2. >> -I N 7. true
PP -QR. -iM2 O 0. PP -IM2 00 0. fa*#e
. -=Q3. -i002. >> .-IM2 00 0. true
i 0 25I 0 75
> -7it(i#e. i > I 2
P -7it(i#e. iPI 7
R-SQR. iRI D
NN 2NN1 C
OO 2OO1 1
OOO i010 -7inary 1010.
iOOO2
2
3
'nternal Functions
decode/R' rever#e# encode?R<
decode/R'Component rever#e# encode?R<...
encode/R' 0 encode# everything e)cept "12
&3&45&#67#89:(); and a*phanu+eric#.
encode/R'Component 0 encode# everything e)cept
89+.4:() and a*phau+eric#.
escape 0 he)adeci+a* #tring encoding. Eoe# not
encode 7518+9: and a*phanu+eric#.
unescape G rever#e# escape
eval 0 eva*uate# JavaScript e)pre##ion#
isNaN 0 true if the argu+ent i# not a nu+7er.
isFinite 0 i#6inite-2'0. return# fa*#e
parse'nt par#e<nt-31.DT. return# 31
parseFloat par#e6*oat-31.DT. return# 31.D
Array (b$ect
len!t< G nu+7er of e*e+ent# in the array
concat G concatenate# argu+ent, return# ne( array.
$oin G return# e*e+ent# a# a #tring #eparated 7y
argu+ent -defau*t i# &.
pop G #uppre## > return *a#t e*e+ent
pus< G add# ne( e*e+ent# to end of array > return#
ne( len!t<.
reverse G invert# order of array e*e+ent#
s<ift G #uppre## > return fir#t e*e+ent
slice G return# array #*ice. 1
#t
arg i# #tart po#ition. 2
nd
arg
i# *a#t po#ition J 1
sort G a*phanu+eric #ort if no argu+ent. $a## #ort
function a# argu+ent for +ore #pecificity.
splice G di#card and rep*ace e*e+ent#
uns<ift G append e*e+ent# to #tart > return ne( len!t<
=ate (b$ect
!et;
!et/C;
set;
set/C;
(here U i# one of Eate, Eay, 6u**Vear, Four#,
:i**i#econd#, :inute#, :onth, Second#, 3i+e,
3i+eHoneQff#et
to=ateStrin! G the date in 2ng*i#h.
to%>Strin! G the date > ti+e in 2ng*i#h.
to?ocale=ateStrin! G the date in the *oca*e *anguage.
to?ocaleStrin! G date > ti+e in the *oca*e *anguage.
to?ocaleimeStrin! G ti+e in the *oca*e *anguage.
toimeStrin! G ti+e in 2ng*i#h.
to/CStrin! G date > ti+e in ?3C, 2ng*i#h
value(f G +i**i#econd# #ince +idnight 01 January 1!70,
?3C
>at< (b$ect
), ?N,@, ?N-, ?(%,@), ?(%-), A', SBR,8-, SBR-
abs G a7#o*ute va*ue
;(n) trigono+etric function#
a;(n) inver#e trigono+etric function#
(here ; i# one of co#, #in or tan
ceil(n) G #+a**e#t (ho*e nu+7er O0 n
e*p(n) G return# e
n
floor(n) 0 7igge#t (ho*e nu+7er N0 n
lo!(n) 0 *ogarith+ of n to the 7a#e e
ma*(n,&n-) G 7igger of n, and n-
min(n,&n-) G #+a**er of n, and n-
poC(a&b) a
b

random G rando+ nu+7er 7et(een 0 and 1
round(n) G n rounded do(n to c*o#e#t integer
sDrt(n) G #Wuare root of n
Number (b$ect
>AE8VA?/) ca 1.7!772J30X
>'N8VA?/) G ca D232C
N)%A'V)8'NF'N'F& A(S''V)8'NF'N'F
n9to)*ponential(m) G n in #cientific notation (ith m
deci+a* p*ace#.
n9toFi*ed() n rounded to the closest (ho*e nu+7er.
n9toArecision(m) G n rounded to m figure#.
Fe)adeci+a* nu+7er# are de#ignated (ith the prefi)
@* or @E9 e.g. 0)66 i# the nu+7er 2DD.
Strin! (b$ect
len!t< G nu+7er of character# in the #tring
s9c<arAt(n) G return# s[n]. n #tart# at 0
s9c<arCodeAt(n) G ?nicode va*ue of s[n]
s9fromC<arCode(n,&n-99) #tring 7ui*t fro+ ?nicode
va*ue# n,, n-...
s,9inde*(f(s-&n) G *ocation of s- in s, #tarting at
po#ition n
s,9last'nde*(f(s-) G *ocation of s- in s, #tarting fro+
the end
s9substr(n,&n -) G return# #u7#tring #tarting fro+ n, upto
character preceding n-. =o n- 0 e)tract ti** end. n, N 0 0
e)tract fro+ end.
s9to?oCerCase() return# s in *o(er ca#e character#
s9to/pperCase() care to gue##Y
JavaScript Quick Reference Card
1.03
)scape SeDuences
Gn ne( *ine, Gr carriage return, Gt G ta7 character,
GG Z character, Z1 apo#trophe, Z11 Wuote
Zu==== G ?nicode character at ====
e.g. Zu2D"; give# the character [
HavaScript in I>?
)*ternal HavaScript
N#cript type0Bte)t'Iava#criptB defer0BdeferB
#rc0B'#cript#'e)p*ainthat.I#BON'#criptO
'nline HavaScript
N#cript type0Bte)t'Iava#criptBO
''your code here
N'#criptO
Comments
1: Co++ent# #panning +u*tip*e *ine# :1
11 Si+p*e, #ing*e *ine, co++ent
Conditional )*ecution
if -Condition. CodeIfTrue5e*#e CodeIfFalse
C
:u*ti*ine Code<fU +u#t 7e p*aced in 7race#, JK
sCitc< -variable.
,
case @a*ue1&Code5
breaL5
case @a*ue2&Code5
7reak5
.....
default"Code;
/
variable can 7e 7oo*ean, nu+7er, #tring or even date.
-condition.2(CodeIfTrue)"(CodeIfFalse)
$arenthe#e# are not nece##ary 7ut advi#a7*e
)rror Iandlin!
>et<od ,"3he onerror event
Mscript type#Nte*t1$avascriptNO
function whenError-+#g,ur*,*ine=o.J
//use parameters to provide meaningful messages
K
CindoC9onerror # whenError
M1scriptO
$*ace thi# code in a separate MscriptO99M1scriptO tag
pair to trap error# occurring in other #cript#. 3hi#
techniWue 7*ock# error# (ithout taking corrective action.
>et<od -"3he try..catc<99finally #tate+ent
function showLogValue-nu+.J
var s = 'No Error';
try
Jif -nu+ N 0. t<roC 17adnu+15
if -nu+ 00 0. t<roC 1Hero15 K
catc< -err.
J # 0 err5
sCitc< -err. J
case 17adnu+1&nu+ 0 nu+5
breaL5
case 1Hero1&nu+ 0 15
breaL5 K
K
[finallyJ alert-\#,:ath.*og-nu+.].5K]
K
3he fina**y 7*ock i# optiona*. 3he t(o techniWue# can 7e
u#ed in concert.
?oopin!
function whileLoop-nu+.J
C<ile -nu+ O 0.
J alert-nu+.5
nu+5K
K
function doLoop-nu+.J
doJ
alert-nu+.5
nu+5
KC<ile -nu+ O 0.5
K
function forLoop-nu+.J
var i5
for -i005iNnu+5iJJ.J
alert-nu+.5
K
K
breaL cau#e# i++ediate ter+ination of the *oop.
*oop #tate+ent# after continue are #kipped and the ne)t
e)ecution of the *oop i# perfor+ed.
function for<n%oop-.J
var #,)5
for -) in document.
J
#0) J 1 0 1 J document\)]5
alert-#.5
K
K
3hi# code i# 7e#t te#ted in Qpera (hich offer# the option
of #topping the #cript at each a*ert. <n p*ace of
document any JavaScript o7Iect or an array can 7e
u#ed to *oop through it# propertie#'e*e+ent#.
return
return cau#e# i++ediate ter+ination of the JavaScript
function. <f no va*ue i# returned, or if return i# +i##ing
the function return type i# undefined9
document (b$ect
body the 7ody of the docu+ent
cooLie read'(rite the docu+ent cookie#
domain G (here (a# the docu+ent #erved fro+Y
forms[] array of a** for+# in the docu+ent
ima!es[] array of a** i+age# in the docu+ent
referrer G (ho pointed to thi# docu+entY
/R? G the ?R% for the docu+ent
!et)lementPy'd(id) G e*e+ent 7earing <E of id
!et)lementsPyName(n) G array of e*e+ent# na+ed n
!et)lementsPya!Name(t) array of t tagged
e*e+ent#
Crite G (rite p*ain or F3:% te)t to the docu+ent
onload G occur# (hen the docu+ent i# *oaded
onunload G occur# (hen u#er 7ro(#e# a(ay, ta7 i#
c*o#ed etc.
)lement (b$ect
"y e*e+ent (e +ean any F3:% e*e+ent retrieved u#ing
the document.!et)lementPy; +ethod#.
attributes G a** e*e+ent attri7ute# in an array
className G the CSS #ty*e a##igned to the e*e+ent
id G the id a##igned to the e*e+ent
innerI>? G F3:% content of the e*e+ent
innere*t G content of the e*e+ent #horn of a** F3:%
tag#. Eoe# not (ork in 6irefo)
offset; G e*e+ent di+en#ion# -; # Iei!<t1Qidt<. or
*ocation-; # ?eft1Ri!<t. in pi)e*#
oCner=ocument G take a gue##
style G CSS #ty*e dec*aration
ta!Name G e*e+ent tag type. Curiou#*y, a*(ay# in
upperca#e
te*tContent G the 6irefo) eWuiva*ent of innere*t
location (b$ect
<ost G ?R% of the #ite #erving up the docu+ent
<ref G the entire ?R% to the docu+ent
pat<name G the path to the docu+ent on the ho#t
protocol G the protoco* u#ed, e.g. http
reload(p) re*oad the docu+ent. 6ro+ the cache if p i#
true.
replace(url) G rep*ace the current docu+ent (ith the
one at url9 Ei#card docu+ent entry in 7ro(#er hi#tory.
screen (b$ect
<ei!<t G #creen height in pi)e*#
Cidt< G #creen (idth in pi)e*#
CindoC (b$ect
alert(ms!) G di#p*ay# a dia*og (ith ms!
clear'nterval(id) G c*ear# interva* id #et 7y #et<nterva*
clearimeout(id) G c*ear# ti+eout id #et 7y #et3i+eout
confirm(ms!) G #ho(# a confir+ation dia*og
print() print# the (indo( content#
prompt(ms!& [default ]) G #ho(# pro+pt dia*og,
optiona**y (ith defau*t content. Return# content or null.
set'nterval(e*pr&interval) G #et# repeat at interval +#.
3he function e*pr i# eva*uated
R
.
setimeout(e*pr&time) %ike set'nterval 7ut non
repeating.
R
Notes
,
2va*uate# after u#e
-
2va*uate# before u#e
3
^erofi** right #hift
C
=ote the #e+ico*on4
R
$a##ing argu+ent# to function ca**# via e*pr i# not (e**
#upported.
Color Codin!
italics u#er code blue 0 JavaScript _ey(ord#
red G Qption ob$ect G JavaScript EQ: o7Iect
!reen 0 on*y nu+eric va*ue# blue + o7Iect propertie#
!reen G o7Iect +ethod# ma!enta G o7Iect event#
3e#ted (ith <nternet 2)p*orer LJ, 6irefo) 1.DJ > Qpera
!.1J.