[ Web Proxy ]
URL:
Viewing: https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Statements/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 declaration) (parameter) .

Function (function expression) .

In this article

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

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

js
    function name([param[, param,[..., param]]]) { [statements] }
name

.

param

(argument) . .

statements

(body) (statement).

Function , Function (property), (behavior) . Function .

( ) .

undefined . , return .

. , function if . , . , (function expression) .

js
var hoisted = "foo" in this;
console.log(
  `'foo' name ${
    hoisted ? "is" : "is not"
  } hoisted. typeof foo is ${typeof foo}`,
);
if (false) {
  function foo() {
    return 1;
  }
}

// In Chrome:
// 'foo' name is hoisted. typeof foo is undefined
//
// In Firefox:
// 'foo' name is hoisted. typeof foo is undefined
//
// In Edge:
// 'foo' name is not hoisted. typeof foo is undefined
//
// In Safari:
// 'foo' name is hoisted. typeof foo is function

.

js
var hoisted = "foo" in this;
console.log(
  `'foo' name ${
    hoisted ? "is" : "is not"
  } hoisted. typeof foo is ${typeof foo}`,
);
if (true) {
  function foo() {
    return 1;
  }
}

// In Chrome:
// 'foo' name is hoisted. typeof foo is undefined
//
// In Firefox:
// 'foo' name is hoisted. typeof foo is undefined
//
// In Edge:
// 'foo' name is not hoisted. typeof foo is undefined
//
// In Safari:
// 'foo' name is hoisted. typeof foo is function

JavaScript (global scope) .

js
hoisted(); // logs "foo"

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

:

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

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

function

a, b c , .

js
function calc_sales(units_a, units_b, units_c) {
  return units_a * 79 + units_b * 129 + units_c * 699;
}

Specification
ECMAScript 2027 LanguageSpecification
# sec-function-definitions


Web Proxy Viewer  |  New URL  |  Original Page