Helping ordinary people create extraordinary websites!
HOME TUTORIALS SCRIPTS WEB HOSTING BLOG FORUM
Get Our Newsletter
Email:

JavaScript Tutorial Part II - Function Basics

By Will Bontrager
2004-06-14


Using/Calling functions

When you call a function, the browser runs the lines of code in the function. Whether the function is built-in or one you made, call it in your program by typing the function's name.

A function name always includes a pair of parenthesis at the end. There might or might not be something between the parenthesis.

If you want to send data to a function when you call it, put the data between the parenthesis. For example,

alert("Hello everybody!")

is a function call that displays an alert box proclaiming: Hello everybody!

If there is more than one unit of data to send to the function, separate them with commas. (A unit of data can be either a number or a string of characters, the latter being enclosed between single or double quotes and being called a "string" in most programming languages with which I am familiar.)

If you send more data units than the function will use, the excess is ignored. The alert() function expects only one data unit. So sending more than one, such as

alert("Hello everybody!","extra")

will cause only the first unit to be displayed in an alert box: Hello everybody!

On the other hand, if the units of data you send to a function numbers less than the function expects, the function will assume the rest is undefined. Trying to do calculations with something undefined usually produces an error message. Printing something that is undefined will usually produce the word "undefined". For example,

alert();

will display an alert box with: undefined

To find out how many units of data a built-in function expects to receive, you can consult documentation or emulate how someone else used it. The first method is more certain to be correct while the latter may be faster with sufficient certainty for the job at hand.

Links to JavaScript reference manuals and sites are at
http://search.netscape.com/Computers/Programming/Languages/JavaScript/References

Tutorial Pages:
» JavaScript Tutorial Part II - Function Basics
» Using/Calling functions
» How to make your own functions


Copyright 2004 Bontrager Connection, LLC


 | Bookmark
Related Tutorials:
» JavaScript Debugging Techniques with Firebug
» Striped Tables Using JavaScript
» Opening PDFs in a New Window with JavaScript
» Essential Javascript -- A Javascript Tutorial
» Submit Forms Conditionally using JavaScript
» How to Setup a Randomising Function

Ask A Question
characters left.