Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

javascript
communitycreator

How to find the number of parameters expected by a JS function

Gutha Vamsi Krishna

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:

  1. multiply: It takes 2 parameters and returns their multiplication.
  2. 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)

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.

RELATED TAGS

javascript
communitycreator
RELATED COURSES

View all Courses

Keep Exploring