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 functionfunction multiply(a, b){return a * b;}//get the number of parameters for function multiplyconsole.log(multiply.length)//define a functionfunction sum(num1, num2, num3, num4){return num1 + num2 + num3 + num4;}//get the number of parameters for function sumconsole.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.