In this article you learn how to create basic Java Script functions. You learn how to use parameters and how to return values, including how to return an array from a function. The article also intoduces variable scope.
Welcome to Bucaro TecHelp!

Bucaro TecHelp
Maintain Your Computer and Use it More Effectively
to Design a Web Site and Make Money on the Web

About Bucaro TecHelp About BTH User Agreement User Agreement Privacy Policy Privacy Site Map Site Map Contact Bucaro TecHelp Contact RSS News Feeds News Feeds

Victims of Sandy Hook

Stop the Slaughter of Innocents. Congress is bought and paid for by gun lunatics and gun promotion groups. If you want to live in a safe America, help buy Congress back for America. Send a donation to Mayors Against Illegal Guns, 909 Third Avenue, 15th Floor New York, NY 10022

Creating Basic Java Script Functions

A function is a group of programming statements and⁄or structures that are not performed until you call the function by its name, which is called "executing" the function. From the outside, you can view a function as a "black box". You put something in one end of the black box (called "passing it to the function"), and something comes out the other end of the black box (called "returned by the function").

Inside the black box, the function was performed. But many times a programmer doesn't know or doesn't even care what statements and⁄or structures are inside the black box, as long as the box works properly every time.

Java Script has many built-in functions, for example the eval() function. You pass a mathematical expression as a string (computerese for a sentence inside quotes) to the eval() function, and it returns the result of the expression. For example, the statement below passes the string "2 * 6" to the eval() function and it returns the number 12, placing it in a variable named "result".

var result = eval("2 * 6");

Java Script has hundreds of built-in functions and before you create your own function, it would be wise to see if there is a built-in function you can use instead. If you need to create your own function (called "declaring a function" or creating a function "declaration"), the syntax for creating a function is shown below.

function function_name ([parameter], ... ,[parameter])
   // statements and/or structures

Start your function declaration with the keyword "function", then follow that with the function's name, followed by parenthesis (), followed by curly braces {}. The function's name must NOT be a Java Script keyword (or "reserved word"). Within the parenthesis you place the names of one or more values (or "parameters"), separated by commas, that you want to pass to the function. Within the curly braces you place the code that actually performs the operations on the parameters.

Note that the last statement in the code is "return". This is actually optional because unless an error occurs within the function code, the function IS going to return. Return means "return to the main programs flow structure". Return can also mean "return a value". Let's create a few basic example functions.

If you want to follow along, open Windows Notepad (Start | All Programs | Accessories | Notepad), or your favorite ASCII text editor (Don't use a fancy word processing application like Microsoft Word, because they add loads of formatting tags). Type in the text shown below and then save the file with the extension .htm.

<script type="text/javascript">

alert("Hello World");


Lets call this example1.htm. Then double-click on the file and it should open in your default web browser and a message box should appear displaying the message "Hello World". If that worked for you, congratulations, you just created your first Java Script program. If it didn't work, make sure you're not using a word processing application and check for typos. You might want to drag a shortcut to Notepad to your desktop for use with future examples.

RSS Feed RSS Feed

Follow Stephen Bucaro Follow @Stephen Bucaro

Web Design Sections

Fire HD
[Site User Agreement] [Privacy Policy] [Site map] [Search This Site] [Contact Form]
Copyright©2001-2017 Bucaro TecHelp 13771 N Fountain Hills Blvd Suite 114-248 Fountain Hills, AZ 85268