The length
property of a function denotes the number
of arguments a function expects.
The returned number excludes the
function test(a, b, c){console.log("hi")}console.log("The number argument expected by test function is ")console.log(test.length);
In the code above, we created a test
function with 3
arguments, (a,b,c)
. We checked the number of arguments expected by the test
function through the length
property of the function
object. The test.length
will return 3
because it expects 3
arguments.
function test(a, b = 10, ...c) {console.log(a, b, c);}console.log("The number argument expected by test function is ")console.log(test.length);
In the code above, we created a test
function with 3
arguments, (a, b=10, ...c)
, in which b
is the default parameter and c
is the rest parameter.
The length
property of the function
object doesn’t include the rest
and default
parameters, so the test.length
will return 1
.
arguments.length
and function.length
The arguments.length
denotes the number of arguments actually passed to the function, whereas the function.length
denotes the number of arguments expected by the function.