Function.length

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.

length プロパティは、関数が期待する引数の数を示します。

試してみましょう

function func1() {}

function func2(a, b) {}

console.log(func1.length);
// Expected output: 0

console.log(func2.length);
// Expected output: 2
Function.length のプロパティ属性
書込可能不可
列挙可能不可
設定可能

解説

length は function オブジェクトのプロパティであり、関数が期待する引数の数、つまり形式上の引数の数を示します。この数に残余引数は含まれず、既定値を持つ引数が最初に登場する前までしか含みません。これに対し、 arguments.length は関数のローカルスコープ内で用いられ、関数が実際に受け取った引数の数、つまり実引数の数を参照するのに用いるものです。

Function コンストラクターのデータプロパティ

Function コンストラクター自体は、Function オブジェクトです。この length データプロパティの値は 1 です。プロパティの属性は、 Writable: false、Enumerable: false、Configurable: true です。

Function プロトタイプのオブジェクトのプロパティ

Function プロトタイプオブジェクトの length プロパティの値は 0 です。

関数の length の使用

js
console.log(Function.length); /* 1 */

console.log(function () {}.length); /* 0 */
console.log(function (a) {}.length); /* 1 */
console.log(function (a, b) {}.length); /* 2 etc. */

console.log(function (...args) {}.length);
// 0, rest parameter は数に含まれない

console.log(function (a, b = 1, c) {}.length);
// 1, 既定値を持つ引数が最初に登場する前までの
// 引数だけが数に含まれる

仕様書

Specification
ECMAScript® 2025 Language Specification
# sec-function-instances-length

ブラウザーの互換性

Report problems with this compatibility data on GitHub
desktopmobileserver
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
Deno
Node.js
length
Configurable: true

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support

関連情報