JavaScript Function.length property
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:
<!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 paramters required by "+ "the function are: ", func1.length) </ script > </ body > </ html > |
Output:
Example 2:
When number of parameters is greater than one.
<!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 paramters required by the func1 are: ", func1.length) // When two parameters are given function func2(a, b){} console.log( "The number of paramters required by the func2 are: ", func2.length) // When three parameters are given function func3(a, b, c){} console.log( "The number of paramters required by the func3 are: ", func3.length) // When four parameters are given function func4(a, b, c, d){} console.log( "The number of paramters required by the func4 are: ", func4.length) </ script > </ body > </ html > |
Output:
Example 3:
When array of arguments is given
<!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 paramters required by the func4 are: ", func4.length) </ script > </ body > </ html > |
Output: