[ Web Proxy ]
URL:
Viewing: https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Statements/function [Back]  [Original]

function - JavaScript | MDN

MDN Web Docs

View in English Always switch to English

function

Baseline

20157

function

function

function calcRectArea(width, height) {
  return width * height;
}

console.log(calcRectArea(5, 6));
// : 30

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

name

param

statements

Function return undefined

function var let

:

if

js
console.log(
  `'foo' ${
    "foo" in globalThis ? "" : ""
  } typeof foo  ${typeof foo} `,
);
if (false) {
  function foo() {
    return 1;
  }
}

// Chrome :
// 'foo'  typeof foo  undefined 
//
// Firefox :
// 'foo'  typeof foo  undefined 
//
// Safari :
// 'foo'  typeof foo  function 

if

js
console.log(
  `'foo' ${
    "foo" in globalThis ? "" : ""
  } typeof foo  ${typeof foo} `,
);
if (true) {
  function foo() {
    return 1;
  }
}

// In Chrome:
// 'foo'  typeof foo  undefined 
//
// In Firefox:
// 'foo'  typeof foo  undefined 
//
// In Safari:
// 'foo'  typeof foo  function 

js
"use strict";

{
  foo(); // "foo" 
  function foo() {
    console.log("foo");
  }
}

console.log(
  `'foo' ${
    "foo" in globalThis ? "" : ""
  } typeof foo  ${typeof foo} `,
);
// 'foo'  typeof foo  undefined 

JavaScript

js
hoisted(); // "foo" 

function hoisted() {
  console.log("foo");
}

js
notHoisted(); // TypeError: notHoisted is not a function

var notHoisted = function () {
  console.log("bar");
};

function

function var function var letconstclass

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

function var var

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

function var

js
function foo(a) {
  function a() {}
  console.log(typeof a);
}

foo(2); // : "function"

function let

js
// 
function foo() {}
function foo() {} // SyntaxError: Identifier 'foo' has already been declared
js
"use strict";
{
  function foo() {}
  function foo() {} // SyntaxError: Identifier 'foo' has already been declared
}

catch function catch

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

function

3

js
function calcSales(unitsA, unitsB, unitsC) {
  return unitsA * 79 + unitsB * 129 + unitsC * 699;
}

ECMAScript 2027 LanguageSpecification
# sec-function-definitions


Web Proxy Viewer  |  New URL  |  Original Page