| [ Web Proxy ] |
| Viewing: https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Statements/function | [Back] [Original] |
Get to know MDN better
function calcRectArea(width, height) {
return width * height;
}
console.log(calcRectArea(5, 6));
// Expected output: 30
function name(param0) {
statements
}
function name(param0, param1) {
statements
}
function name(param0, param1, /* , */ paramN) {
statements
}
function Function return undefined
let let var globalThis function var 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
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
"use strict";
{
foo(); // "foo"
function foo() {
console.log("foo");
}
}
console.log(
`foo${
"foo" in globalThis ? "" : ""
}typeof foo ${typeof foo}`,
);
// footypeof foo undefined
hoisted(); // "foo"
function hoisted() {
console.log("foo");
}
notHoisted(); // TypeError: notHoisted is not a function
var notHoisted = function () {
console.log("bar");
};
function
function var function var letconst class
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
var a = 1;
function a() {}
console.log(a); // 1
function var
function foo(a) {
function a() {}
console.log(typeof a);
}
foo(2); // "function"
function let
//
function foo() {}
function foo() {} // SyntaxError: Identifier 'foo' has already been declared
"use strict";
{
function foo() {}
function foo() {} // SyntaxError: Identifier 'foo' has already been declared
}
catch function catch
try {
} catch (e) {
function e() {} // SyntaxError: Identifier 'e' has already been declared
}
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 |