Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
100% found this document useful (1 vote)
384 views

JavaScript Quick Reference Cheat Sheet

Tack this reference up on your cork board so that it never leaves your field of vision!
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
384 views

JavaScript Quick Reference Cheat Sheet

Tack this reference up on your cork board so that it never leaves your field of vision!
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 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.1C1DE, 100, 0...
boolean" var fla! 0 fa*#e or true
ob$ect" var d 0 ne( Fate-.5
function" var %reet # function #ayGe**o-. ,a*ert-1Ge**o1./
JavaScript i# a (eak*y typed *anguage H 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 recogniIed. null i# an o7Ject. 'nfinity
and NaN are nu+7er#.
(perators
(perator )*ample Result
K 3 K 2
1e)p*ain1 K 1that1
D
e)p*ainthat
+ 3 2 1
L 3L2 M
' 3'2 1.D
N 3N2 1
KK i 0 25
iKK
,
, KKi
-
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
O 2 O 3
1a1 O 1;1
true
fa*#e
O0 2 O0 3 true
P 2 P 3 fa*#e
P0 2 P 3 fa*#e
0 i 0 2 i i# a##igned
the va*ue 2
K0 iK01 3
0 i01 2
iL0 iL03 M
'0 i'02 3
N0 iN02 1
i 0 25J 0 D5
>> -;=F. -i O0 2. >> -J O 7. true
QQ -RR. -iN2 P 0. QQ -JN2 00 0. fa*#e
. -=R3. -i002. >> .-JN2 00 0. true
i 0 25J 0 75
> -7it(i#e. i > J 2
Q -7it(i#e. iQJ 7
S-TRR. iSJ D
OO 2OO1 C
PP 2PP1 1
PPP i010 -7inary 1010.
iPPP2
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. Foe# not
encode 7518+9: and a*phanu+eric#.
unescape H 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.DU. return# 31
parseFloat par#e6*oat-31.DU. return# 31.D
Array (b$ect
len!t< H nu+7er of e*e+ent# in the array
concat H concatenate# argu+ent, return# ne( array.
$oin H return# e*e+ent# a# a #tring #eparated 7y
argu+ent -defau*t i# &.
pop H #uppre## > return *a#t e*e+ent
pus< H add# ne( e*e+ent# to end of array > return#
ne( len!t<.
reverse H invert# order of array e*e+ent#
s<ift H #uppre## > return fir#t e*e+ent
slice H return# array #*ice. 1
#t
arg i# #tart po#ition. 2
nd
arg
i# *a#t po#ition K 1
sort H a*phanu+eric #ort if no argu+ent. $a## #ort
function a# argu+ent for +ore #pecificity.
splice H di#card and rep*ace e*e+ent#
uns<ift H append e*e+ent# to #tart > return ne( len!t<
=ate (b$ect
!et;
!et/C;
set;
set/C;
(here V i# one of Fate, Fay, 6u**Wear, Gour#,
:i**i#econd#, :inute#, :onth, Second#, 3i+e,
3i+eIoneRff#et
to=ateStrin! H the date in 2ng*i#h.
to%>Strin! H the date > ti+e in 2ng*i#h.
to?ocale=ateStrin! H the date in the *oca*e *anguage.
to?ocaleStrin! H date > ti+e in the *oca*e *anguage.
to?ocaleimeStrin! H ti+e in the *oca*e *anguage.
toimeStrin! H ti+e in 2ng*i#h.
to/CStrin! H date > ti+e in ?3C, 2ng*i#h
value(f H +i**i#econd# #ince +idnight 01 January 1E70,
?3C
>at< (b$ect
), ?N,@, ?N-, ?(%,@), ?(%-), A', SBR,8-, SBR-
abs H 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) H #+a**e#t (ho*e nu+7er P0 n
e*p(n) H return# e
n
floor(n) 0 7igge#t (ho*e nu+7er O0 n
lo!(n) 0 *ogarith+ of n to the 7a#e e
ma*(n,&n-) H 7igger of n, and n-
min(n,&n-) H #+a**er of n, and n-
poC(a&b) a
b

random H rando+ nu+7er 7et(een 0 and 1
round(n) H n rounded do(n to c*o#e#t integer
sDrt(n) H #Xuare root of n
Number (b$ect
>AE8VA?/) ca 1.7E772K30!
>'N8VA?/) H ca D232C
N)%A'V)8'NF'N'F& A(S''V)8'NF'N'F
n9to)*ponential(m) H n in #cientific notation (ith m
deci+a* p*ace#.
n9toFi*ed() n rounded to the closest (ho*e nu+7er.
n9toArecision(m) H n rounded to m figure#.
Ge)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< H nu+7er of character# in the #tring
s9c<arAt(n) H return# s[n]. n #tart# at 0
s9c<arCodeAt(n) H ?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) H *ocation of s- in s, #tarting at
po#ition n
s,9last'nde*(f(s-) H *ocation of s- in s, #tarting fro+
the end
s9substr(n,& n -) H return# #u7#tring #tarting fro+ n, upto
character preceding n-. =o n- 0 e)tract ti** end. n, O 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 H ta7 character,
GG Z character, Z1 apo#trophe, Z11 Xuote
Zu==== H ?nicode character at ====
e.g. Zu2D"; give# the character [
HavaScript in I>?
)*ternal HavaScript
O#cript type0Bte)t'Java#criptB defer0BdeferB
#rc0B'#cript#'e)p*ainthat.J#BPO'#criptP
'nline HavaScript
O#cript type0Bte)t'Java#criptBP
''your code here
O'#criptP
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<fV +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#
techniXue 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+ O 0. t<roC 17adnu+15
if -nu+ 00 0. t<roC 1Iero15 K
catc< -err.
J # 0 err5
sCitc< -err. J
case 17adnu+1&nu+ 0 nu+5
breaL5
case 1Iero1&nu+ 0 15
breaL5 K
K
[finallyJ alert-\#,:ath.*og-nu+.].5K]
K
3he fina**y 7*ock i# optiona*. 3he t(o techniXue# can 7e
u#ed in concert.
?oopin!
function whileLoop-nu+.J
C<ile -nu+ P 0.
J alert-nu+.5
nu+5K
K
function doLoop-nu+.J
doJ
alert-nu+.5
nu+5
KC<ile -nu+ P 0.5
K
function forLoop-nu+.J
var i5
for -i005iOnu+5iKK.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) K 1 0 1 K document\)]5
alert-#.5
K
K
3hi# code i# 7e#t te#ted in Rpera (hich offer# the option
of #topping the #cript at each a*ert. <n p*ace of
document any JavaScript o7Ject 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 H (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 H (ho pointed to thi# docu+entY
/R? H the ?R% for the docu+ent
!et)lementPy'd(id) H e*e+ent 7earing <F of id
!et)lementsPyName(n) H array of e*e+ent# na+ed n
!et)lementsPya!Name(t) array of t tagged
e*e+ent#
Crite H (rite p*ain or G3:% te)t to the docu+ent
onload H occur# (hen the docu+ent i# *oaded
onunload H occur# (hen u#er 7ro(#e# a(ay, ta7 i#
c*o#ed etc.
)lement (b$ect
"y e*e+ent (e +ean any G3:% e*e+ent retrieved u#ing
the document.!et)lementPy; +ethod#.
attributes H a** e*e+ent attri7ute# in an array
className H the CSS #ty*e a##igned to the e*e+ent
id H the id a##igned to the e*e+ent
innerI>? H G3:% content of the e*e+ent
innere*t H content of the e*e+ent #horn of a** G3:%
tag#. Foe# not (ork in 6irefo)
offset; H e*e+ent di+en#ion# -; # Iei!<t1Qidt<. or
*ocation-; # ?eft1Ri!<t. in pi)e*#
oCner=ocument H take a gue##
style H CSS #ty*e dec*aration
ta!Name H e*e+ent tag type. Curiou#*y, a*(ay# in
upperca#e
te*tContent H the 6irefo) eXuiva*ent of innere*t
location (b$ect
<ost H ?R% of the #ite #erving up the docu+ent
<ref H the entire ?R% to the docu+ent
pat<name H the path to the docu+ent on the ho#t
protocol H the protoco* u#ed, e.g. http
reload(p) re*oad the docu+ent. 6ro+ the cache if p i#
true.
replace(url) H rep*ace the current docu+ent (ith the
one at url9 Fi#card docu+ent entry in 7ro(#er hi#tory.
screen (b$ect
<ei!<t H #creen height in pi)e*#
Cidt< H #creen (idth in pi)e*#
CindoC (b$ect
alert(ms!) H di#p*ay# a dia*og (ith ms!
clear'nterval(id) H c*ear# interva* id #et 7y #et<nterva*
clearimeout(id) H c*ear# ti+eout id #et 7y #et3i+eout
confirm(ms!) H #ho(# a confir+ation dia*og
print() print# the (indo( content#
prompt(ms!& [ default ] ) H #ho(# pro+pt dia*og,
optiona**y (ith defau*t content. Return# content or null.
set'nterval(e*pr&interval& [ ar!s ] ) H #et# repeat at
interval +#. 3he function e*pr i# eva*uated, optiona**y
(ith ar!s pa##ed a# para+eter#.
setimeout(e*pr&time& [ ar!s ] ) %ike set'nterval 7ut
nonrepeating.
Notes
,
2va*uate# after u#e
-
2va*uate# before u#e
3
^erofi** right #hift
C
=ote the #e+ico*on4
Color Codin!
italics u#er code blue 0 JavaScript _ey(ord#
red H Rption ob$ect H JavaScript FR: o7Ject
!reen 0 on*y nu+eric va*ue# blue + o7Ject propertie#
!reen H o7Ject +ethod# ma!enta H o7Ject event#
3e#ted (ith <nternet 2)p*orer MK, 6irefo) 1.DK > Rpera
E.1K.
<f you find thi# reference card u#efu* p*ea#e he*p u# 7y
creating *ink# to our #ite http&''(((.e)p*ainth.at (here
you (i** find other Xuick reference card# and +any other
free progra++ing re#ource#.

You might also like