This guide covers the following sections:
A function is a JavaScript procedure a set of statements that performs a specific task or returns the result of a calculation or comparison.
Functions combine several operations under one name. We can write out a set of statements, name it, and then execute the entire set at any time we need to, by calling it and passing to it any information it needs.
We pass information to a function by enclosing the information in parentheses after the name of the function. Bits of information passed to a function are called arguments or parameters. Functions can take one or more arguments, or none at all.
JavaScript supports two kinds of functions: those that are built into the language, and those that we create.
The JavaScript language includes several built-in functions. Some of them let you handle expressions and special characters, and convert strings to numeric values.
For example, escape ( ) and unescape ( ) are used to convert characters that have special meanings in HTML code, characters that you cannot just put directly into text. For example, the angle brackets, "<" and ">", delineate HTML tags.
The escape function takes as its argument any of these special characters, and returns the escape code for the character. Each escape code consists of a percent sign ( % ) followed by a two-digit number. The unescape function is the exact inverse. It takes as its argument a string consisting of a percent sign and a two-digit number, and returns a character.
Another useful built-in function is eval ( ), which evaluates any valid mathematical expression that is presented in string form. The eval ( ) function takes one argument, the expression to be evaluated.
var anExpression = "6 * 9 % 7";
var total = eval ( anExpression );
// Assigns the value 5 to the variable total.
var yetAnotherExpression = "6 * ( 9 % 7 )";
total = eval ( yetAnotherExpression )
// Assigns the value 12 to the variable total.
var totality = eval ( "...surrounded by acres of clams." );
// Generates an error.
Consult the language reference for more information about these and other built-in functions.
JavaScript allows us to define and use our own functions. A function definition consists of the function keyword, followed by:
- The name of the function.
- A list of arguments that may be passed to the function, enclosed in parentheses and separated by commas.
- The JavaScript statements that specify the operation of the function, enclosed in curly braces { }. The statements in a function can include calls to other functions defined in the current application.
For example, the following code defines a simple function
named square.
function square ( number ) {
return number * number;
}
The function square takes one argument, called number. The function consists of one statement that instructs to return the argument passed to the function multiplied by itself. The return statement specifies the value returned by the function.
return number * number
In addition to defining functions as described here, you can also define Function objects, as described in Function Object.
A method is a function associated with an object. Methods are discussed in Working with Objects.
Defining a function names the function and specifies what to do when the function is called. Calling the function actually performs the specified actions with the indicated parameters. For example, in our example function square, we could call it as follows.
x = square ( 5 )
The statement above calls the function with a parameter of five, which would return the value twenty-five to the variable x.
Parameters are not limited to strings and numbers. We can pass objects to a function, too. The show_props function defined in Objects and Properties is an example of a function that takes an object as an argument.
A function can even be recursive, that is, it can call itself. For example, here is a function that computes factorials:
function factorial ( n ) {
if ( ( n==0 ) || ( n==1 ) )
return 1
else {
result = ( n * factorial ( n-1 ) )
return result
}
}
We could then compute the factorials of one through five as follows:
a=factorial ( 1 ) // returns 1
b=factorial ( 2 ) // returns 2
c=factorial ( 3 ) // returns 6
d=factorial ( 4 ) // returns 24
e=factorial ( 5 ) // returns 120