Favorites

JavaScript function Statement

☕
Enjoying this site?

Help keep it running with a small coffee donation. Every bit of support is appreciated!

Buy us a coffee
function
JavaScript Functions and ClassesThis statement is supported by all modern browsers.
Updated 28th November 2026

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

console.log(greet('Sam'));

function greet(name = 'Guest', ...extras) {
  return `Hello, ${name}` + (extras.length ? ` and ${extras.join(', ')}` : '');
}

console.log(greet());
console.log(greet('Sam', 'Alex', 'Lee'));

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.

This statement is supported by all modern browsers.

Desktop

Chrome
Edge
Firefox
Opera
Safari

Tablets & Mobile

Chrome Android
Firefox Android
Opera Android
Safari iOS
Samsung Internet
Android WebView
-
Supported Unknown Not supported
☕
Enjoying this site?

Help keep it running with a small coffee donation. Every bit of support is appreciated!

Buy us a coffee