Open In App

JavaScript Function length property

Last Updated : 24 May, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

The Javascript 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.

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

Example 1: When the number of parameters is zero.

Javascript




<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>


Output:

The number of parameters required by the function are:  0

Example 2: When the number of parameters is greater than one.

Javascript




<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>


Output:

The number of parameters required by the func1 are:  1
The number of parameters required by the func2 are:  2
The number of parameters required by the func3 are:  3
The number of parameters required by the func4 are:  4

Example 3: When an array of arguments is given

Javascript




<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>


Output:

The number of parameters required by the func4 are:  0

We have a complete list of Javascript Function methods, to check those please go through this Javascript Function Complete reference article.

Supported Browser:

  • Chrome 1 and above
  • Edge 12 and above
  • Firefox 1 and above
  • Internet Explorer 4 and above
  • Opera 3 and above
  • Safari 1 and above


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads