Movatterモバイル変換


[0]ホーム

URL:


MDN Web Docs

function

BaselineWidely available

Thefunction declaration creates abinding of a new function to a given name.

You can also define functions using thefunction expression.

Try it

function calcRectArea(width, height) {  return width * height;}console.log(calcRectArea(5, 6));// Expected output: 30

Syntax

js
function name(param0) {  statements}function name(param0, param1) {  statements}function name(param0, param1, /* …, */ paramN) {  statements}

Parameters

name

The function name.

paramOptional

The name of a formal parameter for the function. Maximum number of arguments varies in different engines. For the parameters' syntax, see theFunctions reference.

statementsOptional

The statements which comprise the body of the function.

Description

Afunction declaration creates aFunction object. Each time when a function is called, it returns the value specified by the last executedreturn statement, orundefined if the end of the function body is reached. Seefunctions for detailed information on functions.

function declarations behave like a mix ofvar andlet:

  • Likelet, in strict mode,function declarations are scoped to the most closely containing block.
  • Likelet, function declarations at the top level of a module or within blocks in strict mode cannot beredeclared by any other declaration.
  • Likevar, function declarations at the top level of a script (strict or non-strict) become properties onglobalThis. Function declarations at the top level of a script or function body (strict or non-strict) can be redeclared by anotherfunction orvar.
  • Like both, function declarations can be re-assigned, but you should avoid doing so.
  • Unlike either, function declarations arehoisted together with its value and can be called anywhere in its scope.

Block-level function declaration

Warning:Innon-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 anif 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 theif 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

Instrict 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

Hoisting

Function declarations in JavaScript arehoisted 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 thatfunction expressions are not hoisted:

js
notHoisted(); // TypeError: notHoisted is not a functionvar notHoisted = function () {  console.log("bar");};

Redeclarations

Whetherfunction 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 likevar and can be redeclared by anotherfunction orvar but not bylet,const, orclass.

js
function a(b) {}function a(b, c) {}console.log(a.length); // 2let a = 2; // SyntaxError: Identifier 'a' has already been declared

Whenfunction declarations are redeclared byvar, thevar 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.

js
var a = 1;function a() {}console.log(a); // 1

At the top level of a function's body,function also behaves likevar and can be redeclared or have the same name as a parameter.

js
function foo(a) {  function a() {}  console.log(typeof a);}foo(2); // Logs "function"

At the top level of a module or a block in strict mode,function declarations behave likelet and cannot be redeclared by any other declaration.

js
// Assuming current source is a modulefunction foo() {}function foo() {} // SyntaxError: Identifier 'foo' has already been declared
js
"use strict";{  function foo() {}  function foo() {} // SyntaxError: Identifier 'foo' has already been declared}

Afunction declaration within acatch block cannot have the same name as thecatch-bound identifier, even in non-strict mode.

js
try {} catch (e) {  function e() {} // SyntaxError: Identifier 'e' has already been declared}

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® 2026 Language Specification
# sec-function-definitions

Browser compatibility

See also

Help improve MDN

Learn how to contribute.

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2025 Movatter.jp