JavaScript Reference ยท Function
Function.length
Reference for JavaScript Function.length: how many parameters a function declares, how defaults and rest parameters change it, and its use in currying.
The length property of a function holds the number of parameters it expects before the first parameter that has a default value or is a rest parameter. It describes the function's declared signature, not how many arguments you actually pass.
Libraries use it to decide how many arguments to wait for, for example when currying a function.
function add(a, b) {} add.length; // 2
Try it
Press Run (or Ctrl + Enter) to see the console output here.
No output.
Press Run (or Ctrl + Enter) to see the console output here.
No output.
Syntax
fn.length
Value: a non-negative integer.
The property is read-only in practice: it is not writable, not enumerable, but it is configurable, so Object.defineProperty() can change it. Assigning with = does nothing (or throws in strict mode).
Usage notes
- Stops at the first default.
function f(a, b = 2, c)has length1, even thoughccomes later without a default. - Rest parameters are not counted.
(a, ...rest)has length1. - Destructured parameters count as one parameter each, defaulted or not (a default stops the count).
- Not the number of arguments passed. Inside a function use
arguments.lengthor a rest parameter to see how many arguments the caller actually supplied. - Bound functions have length
max(0, original.length - boundArgs). - Class constructors: the length of a class is that of its
constructormethod (or0if none is written). - Useful for: currying, partial application, and validation of callbacks, for example detecting whether a callback expects a
doneparameter. - Do not rely on it for behavior that must be exact; minifiers keep it, but code that adds default values changes it.
Browser support
Function.length is available in every browser and JavaScript runtime.