Skip to content
Related Articles

Related Articles

Improve Article

JavaScript Function.length property

  • Last Updated : 21 Jun, 2021

Function.length property of the function object in Javascript is used to return the number of parameters required by a function.

Syntax:

function.length

Parameters: This method requires no parameters.

Return: Return type is number.

Few examples are given below for a better understanding of the method.



Example 1:

html




<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport"
        content="width=device-width,
                initial-scale=1.0">
  <title>Document</title>
</head>
<body>
  <script>
    // Creating function name func
    // When no parameters are given
    function func1(){}
    console.log(
"The number of parameters required by "+
     "the function are: ", func1.length)
  </script>
</body>
</html>

Output:

Example 2:

When number of parameters is greater than one.

html




<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport"
        content="width=device-width,
                 initial-scale=1.0">
  <title>Document</title>
</head>
<body>
  <script>
    // Creating function name func
    // When one parameters are given
    function func1(a){}
    console.log(
"The number of parameters required by the func1 are: ",
  func1.length)
    // When two parameters are given
    function func2(a, b){}
    console.log(
"The number of parameters required by the func2 are: ",
 func2.length)
    // When three parameters are given
    function func3(a, b, c){}
    console.log(
"The number of parameters required by the func3 are: ",
 func3.length)
    // When four parameters are given
    function func4(a, b, c, d){}
    console.log(
"The number of parameters required by the func4 are: ",
 func4.length)
  </script>
</body>
</html>

Output:

Example 3:

When array of arguments is given

html




<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport"
        content="width=device-width,
                 initial-scale=1.0">
  <title>Document</title>
</head>
<body>
  <script>
    // Creating function name func
    // When array of arguments are given
    function func4(...args){}
    console.log(
"The number of parameters required by the func4 are: ",
 func4.length)
  </script>
</body>
</html>

Output:




My Personal Notes arrow_drop_up
Recommended Articles
Page :