The function declaration (function statement) defines a function with the specified parameters.
You can also define functions using the Function
constructor and a function expression
.
function name([param[, param,[..., param]]]) { [statements] }
name
param
statements
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
).
By default, functions return undefined
. To return any other value, the function must have a return
statement that specifies the value to return.
Functions can be conditionally declared, that is, a function statement can be nested within an if
statement, however the results are inconsistent across implementations and therefore this pattern should not be used in production code. For conditional function creation, use function expressions instead.
var hoisted = "foo" in this; console.log(`'foo' name ${hoisted ? "is" : "is not"} hoisted. typeof foo is ${typeof foo}`); if (false) { function foo(){ return 1; } } // In Chrome: // 'foo' name is hoisted. typeof foo is undefined // // In Firefox: // 'foo' name is hoisted. typeof foo is undefined // // In Edge: // 'foo' name is not hoisted. typeof foo is undefined // // In Safari: // 'foo' name is hoisted. typeof foo is function
The results are exactly the same for a condition that evaluates to true
var hoisted = "foo" in this; console.log(`'foo' name ${hoisted ? "is" : "is not"} hoisted. typeof foo is ${typeof foo}`); if (true) { function foo(){ return 1; } } // In Chrome: // 'foo' name is hoisted. typeof foo is undefined // // In Firefox: // 'foo' name is hoisted. typeof foo is undefined // // In Edge: // 'foo' name is not hoisted. typeof foo is undefined // // In Safari: // 'foo' name is hoisted. typeof foo is function
Function declarations in JavaScript are hoisted to the top of the enclosing function or global scope. 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'); };
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; }
Specification | Status | Comment |
---|---|---|
ECMAScript Latest Draft (ECMA-262) The definition of 'Function definitions' in that specification. | Draft | |
ECMAScript 2015 (6th Edition, ECMA-262) The definition of 'Function definitions' in that specification. | Standard | |
ECMAScript 5.1 (ECMA-262) The definition of 'Function definition' in that specification. | Standard | |
ECMAScript 3rd Edition (ECMA-262) The definition of 'Function definition' in that specification. | Standard | |
ECMAScript 1st Edition (ECMA-262) The definition of 'Function definition' in that specification. | Standard | Initial definition. Implemented in JavaScript 1.0. |
Desktop | ||||||
---|---|---|---|---|---|---|
Chrome | Edge | Firefox | Internet Explorer | Opera | Safari | |
Basic support | Yes | Yes | 1 | Yes | Yes | Yes |
Allowed in sloppy mode | 49 | ? | ? | ? | Yes | ? |
Trailing comma in parameters | 58 | ? | 52 | ? | 45 | ? |
Mobile | |||||||
---|---|---|---|---|---|---|---|
Android webview | Chrome for Android | Edge Mobile | Firefox for Android | Opera for Android | iOS Safari | Samsung Internet | |
Basic support | Yes | Yes | Yes | 4 | Yes | Yes | Yes |
Allowed in sloppy mode | ? | ? | ? | ? | ? | ? | ? |
Trailing comma in parameters | 58 | 58 | ? | 52 | 45 | ? | 7.0 |
Server | |
---|---|
Node.js | |
Basic support | Yes |
Allowed in sloppy mode | ? |
Trailing comma in parameters | 8.0.0 |
Functions and function scope
Function
function expression
function* statement
function* expression
Arrow functions
GeneratorFunction
async function
async function expression
© 2005–2018 Mozilla Developer Network and individual contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/function