Checks how many arguments a function takes in Javascript?

Function.length will do the job (really weird, in my opinion)

function test( a, b, c ){}

alert( test.length ); // 3

By the way, this length property is quite useful, take a look at these slides of John Resig’s tutorial on Javascript

EDIT

This method will only work if you have no default value set for the arguments.

function foo(a, b, c){};
console.log(foo.length); // 3


function bar(a="", b = 0, c = false){};
console.log(bar.length); // 0

The .length property will give you the count of arguments that require to be set, not the count of arguments a function has.

Leave a Comment