Zmsl?$'&@JJ"]IkwX丝Ls$,Y "N9w<}Ϳ~`G ==y%vD: <LN=o6WfchuirֵnfJL"{#fv51]aGy lo&,QYµp i>'>^w]wqJI5\_}rZFJ8gQ㛽Sm(ɘ 1gM+'4[mV ?ROHh!p~f1eeT^b_:0<Àe 0QL41SF[Myb#O,r3, 3旣hC jh\JƩҙLVkbp(XA҈"$U\Wi8Y Hf&T*B5~yx#FL`efKpɵBq:SJWcgrPg߼cCIC ljK:@}lѿ%}/N)W"[g}.fpJk4PKtqvKoJN,^b9d@UU@66 <Ա!(,rрLBOd@9)sA=pWy'aa 12%Q@aPMZKؽZ ‹b>Y0<ǜpԤ"5C7Q(H?#5P Q+z5Rf72N鐩T] +@/uD,oܶ2DNrmy5??w;D!9%[D[< 7x98?N+{]]Փfka7Rwͷ Scv#QO[&b ^ˎTȕe8R$@/>%ԓ 5Q^bQ@DJ|s{:.9]P ;!i :CЪj u:lzB:T#=B ^qnSO6(ZO=UNc}ȅ`A"FbD>ۈts¹NLOS${GC 5<Um#ɕ*¯ 0J"  tRf4h.s:c*]dQ .2d%G& f&JY9[;5؝S^nda.tH)|q(21 NNW\CdS见#(m4Q!Ua` uYEp6+bg΢f;uJOJ7<{U9lG_)#b =z`lLmq/<SN a;`j9h JR+Jf]Mkb- (}*JԀ iO\n&m1(f#>Qyr'Vm 1t J"ez(B mq9vc"Y!Q1j9"D|?\gf#$.׬Ok^U5C)P $e*̅yT%iwIAP){Sg5-5Q0>̵ʯ(pB"s_"px46mz\|}-Nil )d }"7#(*k!cC\D`-J`ͥ4։RfP'WJ1 ^to'+v WnRYo0TN":TX{%c0_;l_! [=u`*ª#MKy eNX֙1v rc7v8+%XB~qc+ԸV Js8\l1@Z f n:QGA7Uxͧ sGle9Y06 Lq_ 5O(/ oS.ַg?ǿ9h9Rqz|z@J׈#4Jׇiz}N@7 G?p1mu{OdlS"Ґi-96

JavaScript Introduction

JS Introduction

JavaScript Functions

A function is a reusable code-block that will be executed by an event, or when the function is called.

JavaScript Functions

To keep the browser from executing a script when the page loads, you can put your script into a function.

A function contains code that will be executed by an event or by a call to that function.

You may call a function from anywhere within the page (or even from other pages if the function is embedded in an external .js file).

Functions can be defined both in the <head> and in the <body> section of a document. However, to assure that the function is read/loaded by the browser before it is called, it could be wise to put it in the <head> section.


<script type="text/javascript">
function displaymessage()
alert("Hello World!");
<input type="button" value="Click me!"
onclick="displaymessage()" >

If the line: alert("Hello world!!") in the example above had not been put within a function, it would have been executed as soon as the line was loaded. Now, the script is not executed before the user hits the button. We have added an onClick event to the button that will execute the function displaymessage() when the button is clicked.

You will learn more about JavaScript events in the JS Events chapter.

How to Define a Function

The syntax for creating a function is:

function functionname(var1,var2,...,varX)
some code

var1, var2, etc are variables or values passed into the function. The { and the } defines the start and end of the function.

Note: A function with no parameters must include the parentheses () after the function name:

function functionname()
some code

Note: Do not forget about the importance of capitals in JavaScript! The word function must be written in lowercase letters, otherwise a JavaScript error occurs! Also note that you must call a function with the exact same capitals as in the function name.

The return Statement

The return statement is used to specify the value that is returned from the function.

So, functions that are going to return a value must use the return statement.


The function below should return the product of two numbers (a and b):

function prod(a,b)
return x;

When you call the function above, you must pass along two parameters:


The returned value from the prod() function is 6, and it will be stored in the variable called product.

The Lifetime of JavaScript Variables

When you declare a variable within a function, the variable can only be accessed within that function. When you exit the function, the variable is destroyed. These variables are called local variables. You can have local variables with the same name in different functions, because each is recognized only by the function in which it is declared.

If you declare a variable outside a function, all the functions on your page can access it. The lifetime of these variables starts when they are declared, and ends when the page is closed.

W]s8}3~6biH ٤ iw2r-B?~dB jX:WGHWp9D*tqMȗbN㜦+.RrVT#d>7C2&:գV@{c UPrAÛ@ Xk^gYĬo%4yk-8Mc=s)P !z 3Z-)7B!˜Ώ9bO؇r;nia:op* xKV2tXniL-2tnVx>>٩u֞iRsUkWB5ޕU gB E r/+;eO+K_N,1K5yfE"1Z u1[iR4|#M֣kKZ9Z#~%E~wq 3 m:rګ;Oa3 K/y5nD\*J5α߆'`ĿF