This article is in need of an editorial review.
Summary
Declares a function with the specified parameters.
You can also define functions using the Function
constructor and a function expression
.
Version Information
Statement | |
Implemented in | JavaScript 1.0 |
ECMAScript Edition | ECMA-262 |
Syntax
function name([param,[, param,[..., param]]]) { [statements] }
Parameters
-
name
- The function name.
-
param
- The name of an argument to be passed to the function. A function can have up to 255 arguments.
-
statements
- The statements which comprise the body of the function.
Description
A function created with a function declaration is a Function
object and has all the properties, methods and behavior of Function
objects. See Function
for detailed information on functions.
A function can also be created using an expression (see function expression)
.
Functions can be conditionally declared, that is, a function declaration can be nested within an if
statement. Some browsers will treat such conditional declarations as an unconditional declaration and create the function whether the condition is true or not. Therefore they should not be used.
By default, functions return undefined
. To return any other value, the function must have a return
statement that specifies the value to return.
Examples
Example: Using function
The following code declares a function that returns the total dollar amount of sales, when given the number of units sold of products a
, b
, and c
.
function calc_sales(units_a, units_b, units_c) { return units_a*79 + units_b * 129 + units_c * 699; }
See also
Functions
, Function
, function operator