Skip to content
Tutorials Center
My learning

Topics

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.

JavaScript
function add(a, b) {}
add.length; // 2

Try it

JS Counting declared parameters

Press Run (or Ctrl + Enter) to see the console output here.

No output.

JS A tiny curry helper

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Syntax

JavaScript
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 length 1, even though c comes later without a default.
  • Rest parameters are not counted. (a, ...rest) has length 1.
  • 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.length or 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 constructor method (or 0 if none is written).
  • Useful for: currying, partial application, and validation of callbacks, for example detecting whether a callback expects a done parameter.
  • 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.