about


Scribe:

chrono

blog

Time

science!

book musings

gov

'punk

missle defense

jams

antiques

cultcha blog


Construct:

scripts & programs

on the town

snaps


Self:

creds (PDF)

key

missive


JavaScript Functions

A JavaScript function is a piece of operational code that can be called by the browser. It can be defined once and reused. It can be given data values and it can return results of a calculation.

You define a function with a name. Then you can call the function by the name, along with any inputs (known as parameters) it will need.

The code snippet, below, illustrates how a function works, at its most basic level. This function is called "greeting." It takes one argument, called "Salutation." The user gives it a value (in this case "Hello Betsy") and it displays that message back to the user on the page: In the head:

<script>
var greeting = function(Salutation){
	document.open();
	document.write(Salutation);
	document.close();
	};
</script>
And in the body:
<<body onload="greeting('Hello Betsy')">
Example

* * *

Functions may have multiple parameters:

<script>
var greeting = function(Salutation01, Salutation02, Salutation03){
	document.open();
	document.write(Salutation01);
	document.write(Salutation02);
	document.write(Salutation03);
	document.close();
	};
</script>
...
<body onload="greeting('Hello Betsy', ' and Bob', ' And Frank!')">

Example

* * *

You don't necessarily have to give a JavaScript function a value:

<script>
var greeting = function(){
	document.open();
	document.write("Hello World");
	document.close();
	};
</script>
...
<body onload="greeting()">

Example