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

function - JavaScript | MDN

MDN Web Docs

View in English Always switch to English

function

20157

function

function

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

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

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

name

param

statements

function 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 
// footypeof foo  undefined
//
//  Firefox 
// footypeof foo  undefined
//
//  Safari 
// footypeof foo  function

if

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

//  Chrome 
// footypeof foo  undefined
//
//  Firefox 
// footypeof foo  undefined
//
//  Safari 
// footypeof foo  function

js
"use strict";

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

console.log(
  `foo${
    "foo" in globalThis ? "" : ""
  }typeof foo  ${typeof foo}`,
);
// footypeof 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 letconst class

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
}

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