What is function.length in JavaScript?

The length property of a function denotes the number of arguments a function expects.

The returned number excludes the rest parameterwhich accepts an indefinite number of arguments as an array and default value parameter.

Example 1

function test(a, b, c){
console.log("hi")
}
console.log("The number argument expected by test function is ")
console.log(test.length);

In the code above, we created a test function with 3 arguments, (a,b,c). We checked the number of arguments expected by the test function through the length property of the function object. The test.length will return 3 because it expects 3 arguments.

Example 2

function test(a, b = 10, ...c) {
console.log(a, b, c);
}
console.log("The number argument expected by test function is ")
console.log(test.length);

In the code above, we created a test function with 3 arguments, (a, b=10, ...c), in which b is the default parameter and c is the rest parameter.

The length property of the function object doesn’t include the rest and default parameters, so the test.length will return 1.

Difference between arguments.length and function.length

The arguments.length denotes the number of arguments actually passed to the function, whereas the function.length denotes the number of arguments expected by the function.

New on Educative
Learn to Code
Learn any Language as a beginner
Develop a human edge in an AI powered world and learn to code with AI from our beginner friendly catalog
🏆 Leaderboard
Daily Coding Challenge
Solve a new coding challenge every day and climb the leaderboard

Free Resources