JavaScript function Statement
The function declaration defines a named function that can be called from anywhere in its scope, even before the line where it is written.
Description
A function declaration creates a named function with the given parameters and body. Function declarations are hoisted with their body, so you can call them before they appear in the code. Parameters can have default values, function greet(name = 'Guest'), and a rest parameter, ...args, collects extra arguments into an array.
Functions can also be created as expressions, const f = function () {}, or as arrow functions. Unlike arrow functions, regular functions have their own this, which depends on how they're called, and can be used as constructors with new.
Inside a regular function, the arguments object holds all the values passed in, even ones without a named parameter, although rest parameters are the modern alternative. Functions can be nested, and an inner function keeps access to the variables of the function that created it, even after that outer function has returned. This is called a closure, and it's how JavaScript code commonly keeps private state, such as a counter that only one function can change.
Syntax
function name(param1, param2) {
statements
return value;
}Example
Browser Support
The following information will show you the current browser support for the function statement. Hover over a browser icon to see the version that first introduced support for this statement.
Desktop





Tablets & Mobile





