[ Web Proxy ]
URL:
Viewing: https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Functions [Back]  [Original]

- JavaScript | MDN

MDN Web Docs

View in English Always switch to English

Baseline *

20157

*

() (function body)

JavaScript

JavaScript

Function Function Function typeof "object" "function"

: instanceof Function Function.prototype Function Function.prototype

return return undefined return 1 1

: new

(parameter) (argument) MDN web docs

js
function formatNumber(num) {
  return num.toFixed(2);
}

formatNumber(2);

num (parameter) num JavaScript formatNumber(2) 2 (argument) arguments

js
function updateBrand(obj) {
  // 
  obj.brand = "Toyota";
  // 
  obj = null;
}

const car = {
  brand: "Honda",
  model: "Accord",
  year: 1998,
};

console.log(car.brand); // Honda

// 
updateBrand(car);

// updateBrand  car 
console.log(car.brand); // Toyota

this

JavaScript 4

function, function*, async function, async function*

function, function*, async function, async function*

Function(), GeneratorFunction(), AsyncFunction(), AsyncGeneratorFunction()

new Function.prototype typeof MyClass === "function"

js
// 
const multiply = new Function("x", "y", "return x * y");

// 
function multiply(x, y) {
  return x * y;
} // 

// 
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
  • function
  • Function()
  • arguments this
  • Function()
  • Function()

function

js
const y = function x() {};
console.log(x); // ReferenceError: x is not defined

new Function console.log(new Function().toString())

js
function anonymous(
) {

}

Function() anonymous

js
new Function("alert(anonymous);")();

Function

js
// 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

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

doSomething(
  // 
  function foo() {
    console.log("FOO!");
  },
);

function async function IIFE

js
function () { // SyntaxError: Function statements require a function name
  console.log("FOO!");
}();

function foo() {
  console.log("FOO!");
}(); // SyntaxError: Unexpected token ')'

function void

js
(function () {
  console.log("FOO!");
})();

void function () {
  console.log("FOO!");
}();

js
function myFunc(a, b, c) {
  //  a, b, c 
}

3

js
function myFunc({ a, b }, c = 1, ...rest) {
  //  a, b, c, rest 
}

arguments

arguments

arguments

arguments.callee

arguments.length:

get

set

Object.getOwnPropertyDescriptor() API

ES2015

js
"use strict";

function f() {
  return 1;
}

{
  function f() {
    return 2;
  }
}

f() === 1; // true

//  f() === 2

js
if (shouldDefineZero) {
  function zero() {
    // : 
    console.log("");
  }
}

zero if shouldDefineZero false zero function

js
// var 
// 
var zero;
if (shouldDefineZero) {
  zero = function () {
    console.log("");
  };
}

js
// 
function padZeros(num, totalLen) {
  let numStr = num.toString(); // 
  const numZeros = totalLen - numStr.length; // 
  for (let i = 1; i <= numZeros; i++) {
    numStr = `0${numStr}`;
  }
  return numStr;
}

padZeros

js
let result;
result = padZeros(42, 4); // "0042" 
result = padZeros(42, 2); // "42" 
result = padZeros(5, 4); // "0005" 

typeof window noFunc

js
if (typeof window.noFunc === "function") {
  // noFunc() 
} else {
  // 
}

if noFunc "()"

ECMAScript 2027 LanguageSpecification
# sec-function-definitions


Web Proxy Viewer  |  New URL  |  Original Page