To find the number of expected parameters, we can apply the length
property to the function that returns the length or the number of parameters.
We can use the below syntax to get the number of parameters expected by a function:
function_name.length
In the following example, we create two functions:
multiply
: It takes 2
parameters and returns their multiplication.sum
: It takes 4
parameters and returns the sum of all parameters.//define a function function multiply(a, b){ return a * b; } //get the number of parameters for function multiply console.log(multiply.length) //define a function function sum(num1, num2, num3, num4){ return num1 + num2 + num3 + num4; } //get the number of parameters for function sum console.log(sum.length)
In the code above,
Line 7: We get the number of parameters of function multiply
by calling the length
property on it.
Line 15: We get the number of parameters of function sum
by calling the length
property on it.
RELATED TAGS
CONTRIBUTOR
View all Courses