How to find the number of parameters expected by a JS function
Overview
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.
Syntax
We can use the below syntax to get the number of parameters expected by a function:
function_name.length
Example
In the following example, we create two functions:
multiply: It takes2parameters and returns their multiplication.sum: It takes4parameters 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)
Explanation
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.