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
Syntax
js
function name(param0) {
statements
}
function name(param0, param1) {
statements
}
function name(param0, param1, /* … ,*/ paramN) {
statements
}
name
-
The function name.
param
Optional-
The name of an argument to be passed to the function. Maximum number of arguments varies in different engines.
statements
Optional-
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.
js
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.
js
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.
js
"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:
js
hoisted(); // Logs "foo"
function hoisted() {
console.log("foo");
}
Note that function expressions are not hoisted:
js
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.
js
function calcSales(unitsA, unitsB, unitsC) {
return unitsA * 79 + unitsB * 129 + unitsC * 699;
}
Specifications
Specification |
---|
ECMAScript Language Specification # sec-function-definitions |
Browser compatibility
BCD tables only load in the browser