Get to know MDN better
This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.
The function declaration creates a binding of a new function to a given name.
You can also define functions using the function expression.
The function name.
param OptionalThe name of a formal parameter for the function. Maximum number of arguments varies in different engines. For the parameters' syntax, see the Functions reference.
statements OptionalThe statements which comprise the body of the function.
A function declaration creates a Function object. Each time when a function is called, it returns the value specified by the last executed return statement, or undefined if the end of the function body is reached. See functions for detailed information on functions.
function declarations behave like a mix of var and let:
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.
The scoping and hoisting effect won't change regardless of whether the if body is actually executed.
In strict mode, block-level function declarations are scoped to that block and are hoisted to the top of the block.
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:
Note that function expressions are not hoisted:
Whether function declarations can be redeclared in the same scope depends on what scope it's contained in.
At the top level of a script, function declarations behave like var and can be redeclared by another function or var but not by let, const, or class.
When function declarations are redeclared by var, the var declaration's initializer always overrides the function's value, regardless of their relative position. This is because function declarations are hoisted before any initializer gets evaluated, so the initializer comes later and overrides the value.
At the top level of a function's body, function also behaves like var and can be redeclared or have the same name as a parameter.
At the top level of a module or a block in strict mode, function declarations behave like let and cannot be redeclared by any other declaration.
A function declaration within a catch block cannot have the same name as the catch-bound identifier, even in non-strict mode.
The following code declares a function that returns the total amount of sales, when given the number of units sold of three products.
| ECMAScript® 2027 Language Specification # sec-function-definitions |
Enable JavaScript to view this browser compatibility table.
This page was last modified on Jul 8, 2025 by MDN contributors.
Your blueprint for a better internet.
Visit Mozilla Corporation’s not-for-profit parent, the Mozilla Foundation.
Portions of this content are ©1998–2026 by individual mozilla.org contributors. Content available under a Creative Commons license.