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

- JavaScript | MDN

This page was translated from English by the community. Learn more and join the MDN Web Docs community.

View in English Always switch to English

Baseline Widely available

This feature is well established and works across many devices and browser versions. Its been available across browsers since 2015 7.

function (expression) .

Function (function declaration) .

In this article

const getRectArea = function (width, height) {
  return width * height;
};

console.log(getRectArea(3, 4));
// Expected output: 12

js
    var myFunction = function [name]([param1[, param2[, ..., paramN]]]) { statements };

ES2015 (arrow functions) .

name

. (anonymous) , . .

paramN

(argument) .

statements

(statement).

(function expression) function (syntax) ( function ). function , . IIFE ( ) . .

Function expression

JavaScript . .

js
console.log(notHoisted); // undefined
//even the variable name is hoisted, the definition wasn't. so it's undefined.
notHoisted(); // TypeError: notHoisted is not a function

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

(named)

, . () . arguments.callee .

js
var math = {
  factit: function factorial(n) {
    console.log(n);
    if (n <= 1) return 1;
    return n * factorial(n - 1);
  },
};

math.factit(3); //3;2;1;

name . name . , name ( ) . name ( ) . (arrow functions) ( ).

js
var foo = function () {};
foo.name; // "foo"

var foo2 = foo;
foo2.name; // "foo"

var bar = function baz() {};
bar.name; // "baz"

console.log(foo === foo2); // true
console.log(typeof baz); // undefined
console.log(bar === baz); // false (errors because baz == undefined)

Examples

x . :

js
var x = function (y) {
  return y * y;
};

callback :

js
button.addEventListener("click", function (event) {
  console.log("button is clicked!");
});

Specification
ECMAScript 2027 LanguageSpecification
# sec-function-definitions


Web Proxy Viewer  |  New URL  |  Original Page