JavaScript: Function Length Property
Every function has a own string property "length"
, including:
- Arrow Function
Function
[see Function]Function.prototype
[see Function.prototype]
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);