The syntax to access the length
property is:
func.length
Here, func
is a function.
Return value from Function.length
- Returns the number of formal parameters of the given function.
Example: Using Function.length
function func() {}
console.log(func.length); // 0
function func1(a, b) {}
console.log(func1.length); // 2
function func2(...args) {}
console.log(func2.length); // 0 -> Rest parameters are not counted
function func3(a, b = 10, c) {}
// only parameters before the one with default value are counted
console.log(func3.length); // 1
Output
0 2 0 1
Note: The length
property excludes the rest parameters and only counts parameters until the first one with a default value. In this case, func3.length
returns 1 and skips b(has default value) & c(comes after the default value).