function declaration
The
function declaration defines a function with
the specified parameters.
You can also define functions using the
Function constructor and a
function expression.
Try it
JavaScript Demo: Statement - Function
function calcRectArea(width, height) {
return width * height;
}
console.log(calcRectArea(5, 6));
// Expected output: 30
Syntax
function name(param0) {
statements
}
function name(param0, param1) {
statements
}
function name(param0, param1, /* … ,*/ paramN) {
statements
}
name-
The function name.
paramOptional-
The name of an argument to be passed to the function. Maximum number of arguments varies in different engines.
statementsOptional-
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).
By default, functions return
undefined. To return any other value, the
function must have a return statement that specifies
the value to return.
Block-level function declaration
Warning: In non-strict mode, function declarations inside blocks behave strangely. Only declare functions in blocks if you are in strict mode.
Functions can be conditionally declared — that is, a function statement can be nested within an
if statement. However, in non-strict mode, the results are inconsistent across implementations.console.log(
`'foo' name ${
"foo" in globalThis ? "is" : "is not"
} global. typeof foo is ${typeof foo}`,
);
if (false) {
function foo() {
return 1;
}
}
// In Chrome:
// 'foo' name is global. typeof foo is undefined
//
// In Firefox:
// 'foo' name is global. typeof foo is undefined
//
// In Safari:
// 'foo' name is global. typeof foo is function
The scoping and hoisting effect won't change regardless of whether the
if body is actually executed.console.log(
`'foo' name ${
"foo" in globalThis ? "is" : "is not"
} global. typeof foo is ${typeof foo}`,
);
if (true) {
function foo() {
return 1;
}
}
// In Chrome:
// 'foo' name is global. typeof foo is undefined
//
// In Firefox:
// 'foo' name is global. typeof foo is undefined
//
// In Safari:
// 'foo' name is global. typeof foo is function
In strict mode, block-level function declarations are scoped to that block and are hoisted to the top of the block.
"use strict";
{
foo(); // Logs "foo"
function foo() {
console.log("foo");
}
}
console.log(
`'foo' name ${
"foo" in globalThis ? "is" : "is not"
} global. typeof foo is ${typeof foo}`,
);
// 'foo' name is not global. typeof foo is undefined
Function declaration hoisting
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");
};
Examples
Using function
The following code declares a function that returns the total amount of sales, when
given the number of units sold of three products.
function calcSales(unitsA, unitsB, unitsC) {
return unitsA * 79 + unitsB * 129 + unitsC * 699;
}
Specifications
| Specification |
|---|
| ECMAScript Language Specification # sec-function-definitions |
Browser compatibility
Report problems with this compatibility data on GitHubChrome | Edge | Firefox | Opera | Safari | Chrome Android | Firefox for Android | Opera Android | Safari on iOS | Samsung Internet | WebView Android | Deno | Node.js | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
statement | |||||||||||||
Trailing comma in parameters | |||||||||||||
Legend
Tip: you can click/tap on a cell for more information.
Full support


