Number of ArgumentsLoading saved progress…

Number of Arguments

Implement numberOfArguments(...) — a function that returns how many arguments were actually handed to it at the moment it was called, regardless of how its parameters were declared. This is the call-time argument count, and it is a different thing from a function's declared arity (the number of named parameters you wrote, available as fn.length). A function can declare three parameters and be called with none, or declare none and be called with ten — this question is about counting what came in, not what was named.

Signature

// Accepts any number of arguments of any type.
// Returns the count of arguments passed at the call site.
function numberOfArguments(...args: unknown[]): number;

Examples

numberOfArguments();          // → 0
numberOfArguments(1, 2, 3);   // → 3
numberOfArguments('a');       // → 1
// A passed undefined still counts — it filled an argument slot.
numberOfArguments(undefined);    // → 1
numberOfArguments(null);         // → 1
numberOfArguments(...[1, 2, 3]); // → 3  (spread fills three slots)

Notes

  • Count what is passed, not what is declared. The answer depends only on the call site. Declared parameters that were never passed do not count, and you do not need to declare any named parameters at all.
  • An explicit undefined counts. numberOfArguments(undefined) passes one argument that happens to be undefined. The result is 1, not 0 — the slot was filled.
  • null counts too. Same reasoning: null is a value that occupies an argument slot.
  • Spread fills real slots. numberOfArguments(...[1, 2, 3]) is identical to numberOfArguments(1, 2, 3) — three arguments.
  • Don't confuse this with fn.length. fn.length reports declared arity fixed at definition time; this question asks for the count at call time, which varies per call.
Loading editor…