W3cubDocs

/JavaScript

function declaration

The function declaration (function statement) defines a function with the specified parameters.

You can also define functions using the Function constructor and a function expression.

Try it

Syntax

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.

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

Browser compatibility

Desktop Mobile Server
Chrome Edge Firefox Internet Explorer Opera Safari WebView Android Chrome Android Firefox for Android Opera Android Safari on IOS Samsung Internet Deno Node.js
function
1
12
1
3
3
1
4.4
18
4
10.1
1
1.0
1.0
0.10.0
trailing_comma_in_parameters
58
14
52
No
45
10
58
58
52
43
10
7.0
1.0
8.0.0

See also

© 2005–2022 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/function