The property function.length indicates the number of parameters a function expects
GIVEN reading the length of an old-style defined function (exists since ES1)
WHEN reading the property length of a function without parameters THEN the this returns 0
GIVEN we read the length property of an arrow function
WHEN reading length on a function with two named parameters THEN it returns 2
WHEN function has a single optional parameter THEN length returns 0
WHEN function only has a rest parameter THEN length returns 0
WHEN function has a single named parameter and a rest parameter THEN length returns 1
WHEN function has one named, one optional, and a rest parameter THEN length returns 1
GIVEN reading length where the function has destructured parameters
WHEN the first parameter reads two values using destructuring THEN length is still 1
WHEN two destructured parameters are defined THEN the length is 2
Links
The specification text, which is the same as in the initial introduction of this property, in ES1.
The MDN pages describing this property, easy to read with examples.
Required Knowledge
- `function.length` (as introduced in ES1) (function API)
- `function.length` (with ES5 features) (function API)
- basics (Arrow functions)
- `const` declaration (Block scope)
- as parameter (Rest operator)
- basics (Default parameters)
Related Katas
function API
function.length(as introduced in ES1)function.bind()function.length(with ES5 features)function.length(with ES6 features)function.name
Arrow functions
Async Function
Difficulty Level
INTERMEDIATE
First Published
14 October 2023
Stats
8 tests to solve