Summary
The function statement declares function with the specified parameters.
You can also define functions using the {{jsxref("Function")}} constructor and a {{jsxref("Operators/function", "function expression")}}.
Syntax
function name([param,[, param,[..., param]]]) { [statements] }
-
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 {{jsxref("Function")}} for detailed information on functions.
A function can also be created using an expression (see {{jsxref("Operators/function", "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 {{jsxref("Statements/return", "return")}} statement that specifies the value to return.
Function declaration hoisting
Function declarations in JavaScript are hoisting the function definition. You can use the function before you declared it:
hoisted(); // logs "foo" function hoisted() { console.log("foo"); }
Note that function expressions are not hoisted:
notHoisted(); // TypeError: notHoisted is not a function var notHoisted = function() { console.log("bar"); };
Examples
Example: Using function
The following code declares a function that returns the total 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; }
Specifications
Specification | Status | Comment |
---|---|---|
ECMAScript 1st Edition. | Standard | Initial definition. Implemented in JavaScript 1.0 |
{{SpecName('ES5.1', '#sec-13', 'Function definition')}} | {{Spec2('ES5.1')}} | |
{{SpecName('ES6', '#sec-function-definitions', 'Function definitions')}} | {{Spec2('ES6')}} |
Browser compatibility
{{ CompatibilityTable() }}
Feature | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari |
---|---|---|---|---|---|
Basic support | {{ CompatVersionUnknown() }} | {{ CompatVersionUnknown() }} | {{ CompatVersionUnknown() }} | {{ CompatVersionUnknown() }} | {{ CompatVersionUnknown() }} |
Feature | Android | Chrome for Android | Firefox Mobile (Gecko) | IE Mobile | Opera Mobile | Safari Mobile |
---|---|---|---|---|---|---|
Basic support | {{ CompatVersionUnknown() }} | {{ CompatVersionUnknown() }} | {{ CompatVersionUnknown() }} | {{ CompatVersionUnknown() }} | {{ CompatVersionUnknown() }} | {{ CompatVersionUnknown() }} |
See also
- Functions and function scope
- {{jsxref("Function")}}
- {{jsxref("Operators/function", "function expression")}}