| [ Web Proxy ] |
| Viewing: https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Functions | [Back] [Original] |
Get to know MDN better
This page was translated from English by the community. Learn more and join the MDN Web Docs community.
This feature is well established and works across many devices and browser versions. Its been available across browsers since 2015 7.
* Some parts of this feature may have varying levels of support.
( ) " ". , . , .
return return undefined . return . , .
:
new .
function formatNumber(num) {
return num.toFixed(2);
}
formatNumber(2);
num (parameter) , . JavaScript num . formatNumber(2) 2 (argument). , . arguments .
function updateBrand(obj) {
// .
obj.brand = "";
//
// .
obj = null;
}
const car = {
brand: "",
model: "",
year: 1994,
};
console.log(car.brand); //
// .
updateBrand(car);
// updateBrand car .
console.log(car.brand); //
this , .
JavaScript .
Generator , yield .Promise , await .AsyncGenerator , await yield ..
. Classes new Function.prototype typeof MyClass === "function" .
//
const multiply = new Function("x", "y", "return x * y");
//
function multiply(x, y) {
return x * y;
} // No need for semicolon here
// ; .
const multiply = function (x, y) {
return x * y;
};
// ; .
const multiply = function funcName(x, y) {
return x * y;
};
//
const multiply = (x, y) => x * y;
//
const obj = {
multiply(x, y) {
return x * y;
},
};
.
Function() , function , function , new . . , Generator Generator .function . .Function() . .arguments this .Function() .Function() .function . . , . . ( ). .
const y = function x() {};
console.log(x); // : x .
.
. .
new Function , . console.log(new Function().toString()) .
function anonymous(
) {
}
. Function() anonymous , . . .
new Function("alert(anonymous);")();
. , . Function .
// p .
globalThis.p = 5;
function myFunc() {
// p .
const p = 9;
function decl() {
console.log(p);
}
const expr = function () {
console.log(p);
};
const cons = new Function("\tconsole.log(p);");
decl();
expr();
cons();
}
myFunc();
// Logs:
// 9 ('decl' ( ))
// 9 ('expr' ( ))
// 5 ('cons' ( ))
, Function . new Function(...) . Function .
.
// .
function foo() {
console.log("FOO!");
}
doSomething(
// .
function foo() {
console.log("FOO!");
},
);
. expression statement function async function , IIFEs( ) .
function () { // SyntaxError: Function statements require a function name
console.log("FOO!");
}();
function foo() {
console.log("FOO!");
}(); // SyntaxError: Unexpected token ')'
function . void .
(function () {
console.log("FOO!");
})();
void function () {
console.log("FOO!");
}();
.
function myFunc(a, b, c) {
// You can access the values of a, b, and c here
}
.
function myFunc({ a, b }, c = 1, ...rest) {
// a, b, c .
}
.
"use strict" . syntax error .arguments arguments.callee .. classes getter setter .
. Getter setter Object.getOwnPropertyDescriptor() API .
"use strict";
function f() {
return 1;
}
{
function f() {
return 2;
}
}
f() === 1; // true
// f() === 2
, .
. .
if (shouldDefineZero) {
function zero() {
// :
console.log("zero.");
}
}
. 0 if . shouldDefineZero zero . . .
.
// var .
// .
var zero;
if (shouldDefineZero) {
zero = function () {
console.log("zero.");
};
}
0 .
// 0 .
function padZeros(num, totalLen) {
let numStr = num.toString(); //
const numZeros = totalLen - numStr.length; // 0
for (let i = 1; i <= numZeros; i++) {
numStr = `0${numStr}`;
}
return numStr;
}
padZeros .
let result;
result = padZeros(42, 4); // "0042"
result = padZeros(42, 2); // "42"
result = padZeros(5, 4); // "0005"
typeof . window noFunc . , .
if (typeof window.noFunc === "function") {
// noFunc()
} else {
//
}
if noFunc , () .
| Specification |
|---|
| ECMAScript 2027 LanguageSpecification # sec-function-definitions |
This page was last modified on 2025 10 9 by MDN contributors.
Your blueprint for a better internet.
Portions of this content are 19982026 by individual mozilla.org contributors. Content available under a Creative Commons license.
| Web Proxy Viewer | New URL | Original Page |