We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 1
= w3schools.com manne
[Bearer waschoo'scom [Search]
HOE TL CSS XML sAVASCRIET ASP PHP SQL_MoRE EVEREICES | PXAMDLES | FORUM | ABCLT
Js Basi , - ‘wee HostiNG
2 JavaScript Functions cutwes
35 Introduction ; sesh noctina
1 How To. « Previous Next Chapter »
1$ Whore To Best Hosting Coupons
‘A function will be executed by an event or by a cal to the function. ‘UK Reseller Hosting
‘loud Hosting
JavaScript Functions “Top web Hosting
57.990 SEO Hosting
‘To keep the browser from executing a script when the page loads, you can put your seriptinto a
function. ‘WEB BUILDING
Donnlos HHL E6tor
18 Vaidation
35 Timing
136 Create Object
35 Summary
aS Examples
25 Exemples
35 Objects Examples
35 Browser Exemples
18 DOM Examples
35 Quiz
18 Cerbicate
Js References
Javasuiot objeas
Hie bom Objecss
‘function contains code that will be executed by an event or by 2callto the function.
FREE Webste BUILDER
You may calla function from anywhere within a page (or even from other pages if the function is
embedded in an external 3s fle). Frcs WebteTempates
Functions can be defined both in the chead> and in the section of a document. However, £0. xs editor « trv me!
(gecure that a functon i read|loaded by the browser before ts called could be wise to put
finetona nthe cheac> section. waschoots exass
Sat Cored in
How to Define a Function SYNE PAE: one AS
syntax wascHoots BooKs
‘on Seok:
Wns
funccien functionnane (vari vara, savas Ae
“
Soae coae srarisrics
} Bronser Statics
since ss ay
‘The parameters vard, var2, te, are variables or values passed into the function. The { and the }
fines the stare and end of the function. ‘SHARE THIS PAGE
Note: A function with no parameters must include the parentheses () after the function name, Shece with
"Note: 09 not forget about the importance of capita in Javasart! the word function must be
Whitten in lowercase letters, otherwise a JavaSenpt error occurs! Also note that you must eal 2
function with the exact same capitals a3 in the function name.
JavaScript Function Example
Example
eyneac>
ceoav>
‘Gamput typestbucten* vaiue=*ciiex me!" cneiicr=*azepiaymeazage(}" />
irom
“yeeey>
sssm>
Ifthe fine: alert(“Hello word!) in the example above had not been put within 3 function, ft would
have been’ ‘3: soon a5 the page was loaded. Now, the scripts nol executed before a user
hits the input button. The function displaymessage0 will be executed ifthe input button fs clicked.
You will learn more about JavaScript events in the 35 Events chapter.
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 3 value must use the return statement.
‘The example below retumns the product of two numbers (a and b):
Example
Sacripe typecteext/javascrips">
Fonction preducs (a/b)
‘
,
faeac>
a
Sacripe typecteext/javascrips">
Sccunens write (predic: (4,3)
e/script>
fens
The Lifetime of JavaScript Variables
Ifyou dactare a variable, using "var", within a function, the variable can only be accessed within
that function. When you ext the function, the variable is destroyed. These variables are called local
\Vanables. Youcan have local variables with the sama name in oferent functions, Dacause each i
recognized only bythe function in which itis dedared.
I you declare 2 variable outside 2 function, all the functions on your page can access it. The Ifetme
ofthese variables starts when they are detared, and ends when the page is dosed.
More Examples
unstion with a paramatar
How to pass 3 variable to a function, and use the variable in the function.
undtion that rotuins a value
lw to at's funeton return 2 value.
« Previous Next Chapter »