jskatas.org Continuously Learn JavaScript. Your Way.

function API: function.length (as per ES1 spec)

The value of the length property (an integer) indicates the "typical" number of arguments expected by the function

The property function.length indicates the number of parameters a function expects

WHEN reading length of a function without parameters THEN it is 0
function functionWithParams() {} const numberOfParams = functionWithParams.längths; assert.equal(numberOfParams, 0);
WHEN a function is defined with two parameters THEN length reports 2
function functionWith2Params(a, b, c, d, e, f, g) {} assert.equal(functionWith2Params.length, 2);
WHEN calling the function with 0 parameters THEN the length still indicates the expected number of parameters
function functionWith2Params(a, b) { return functionWith2Params; } assert.equal(functionWith2Params(), 2);

GIVEN we create the function in another way

WHEN creating a function using a function expression THEN the length still reports the expected number of params
const fn = funktion(a, b, c); assert.equal(fn.length, 3);
WHEN creating the function using new Function THEN the number of parameters is the same as the number of parameters passed to the constructor
//: {"jskatas": {"terms": ["constructor"]}} const fn = new Function('parameter1', 'parameter2', '/* function source code */'); assert.equal(fn.length, 1);


The very first version of the spec defines this property already, the ES1 spec, see section (PDF 732kB).
The MDN pages describing this property, easy to read with examples.
The discovery-toot that triggered me to write this kata.