JavaScript Function length property
Last Updated :
24 May, 2023
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
Similar Reads
JavaScript String length Property The JavaScript length property is a fundamental feature used to determine the number of characters in a string. It is essential for various string operations, such as validation, manipulation, and iteration. Understanding how to effectively use the length property can simplify tasks like checking in
2 min read
JavaScript typedArray.length() Method The typedArray.length is an inbuilt property in JavaScript which is used to return the length of the given typedArray. Syntax: typedArray.length Parameters: It does not accept any parameter because it is a property not a function. Return value: It returns the length of the given typedArray. Example
2 min read
JavaScript string.length The string.length is a property in JS which is used to find the length of a given string. The string.length property returns 0 if the string is empty.javascriptlet s1 = 'gfg'; console.log(s1.length); let s2 = ''; console.log(s2.length); let s3 = '#$%A'; console.log(s3.length);Output3 0 4 Syntax:stri
1 min read
HTML DOM length Property The DOM length property in HTML is used to get the number of items in a NodeList object. A NodeList is a collection of child Nodes. For example, the NodeList of the body will contain all the child nodes in the body i.e. paragraphs, comments, headings, script, etc.Syntax: nodelist.lengthReturn Value:
2 min read
JavaScript Array length JavaScript array length property is used to set or return the number of elements in an array. JavaScriptlet a = ["js", "html", "gfg"]; console.log(a.length);Output3 Setting the Length of an ArrayThe length property can also be used to set the length of an array. It allows you to truncate or extend t
2 min read