JavaScript: Function Length Property

By Xah Lee. Date: .

Every function has a own string property "length", including:

Its value is number of parameters excluding parameters that has Argument Default Value, nor counting Rest Parameters .

Basically, value of length is the number of required arguments.

// every function has a own property length

console.log(((x) => x + 1).hasOwnProperty("length"));
console.log(Function.hasOwnProperty("length"));
console.log(Function.prototype.hasOwnProperty("length"));
// value of function length property is number of required arguments

console.log((() => []).length === 0);
console.log(((x) => [x]).length === 1);
console.log(((x, y) => [x, y]).length === 2);

// param with default value is excluded
console.log(((x, y = 4) => [x, y]).length === 1);

// rest param is excluded
console.log(((x, ...y) => [x, y]).length === 1);

JavaScript Function

BUY
ΣJS
JavaScript in Depth

JS Obj Reference

Function

prototype