3

What the docs at Mozilla says:

  console.log((function(...args) {}).length); 
  // 0, rest parameter is not counted

  console.log((function(a, b = 1, c) {}).length);
  // 1, only parameters before the first one with 
  // a default value is counted

So how will I ever be able to count parameters in such cases ?

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/length

10
  • 2
    Counting the number of parameters outside the function or counting the number of arguments passed to the function? Commented Apr 14, 2017 at 16:46
  • 4
    Why do you need to count parameters? And why does your title say "number of arguments"? Those are different things. Commented Apr 14, 2017 at 16:47
  • 1
    Why would you ever care about the number of parameters? Commented Apr 14, 2017 at 16:51
  • 2
    @Bergi I am learning JS and there is nothing wrong in exploring everything , in the future I may require it ! :) Commented Apr 14, 2017 at 16:53
  • 2
    FYI, there is a difference between counting the expected length of the arguments and the length of arguments passed... Commented Apr 14, 2017 at 16:55

1 Answer 1

7

There are two separate things going on here with parameters defined and arguments actually passed.

For a defined function, you can get access to the number of parameters that are defined in the definition of the function with fn.length:

function talk(greeting, delay) {
    // some code here
}

console.log(talk.length);     // shows 2 because there are two defined parameters in 
                              // the function definition

Separately, from within a function, you can see how many arguments are actually passed to a function for a given invocation of that function using arguments.length. Suppose you had a function that was written to accept an optional callback as the last argument:

function talk(greeting, delay, callback) {
    console.log(arguments.length);     // shows how many arguments were actually passed
}

talk("hello", 200);    // will cause the function to show 2 arguments are passed
talk("hello", 200, function() {    // will show 3 arguments are passed
     console.log("talk is done now");
});                                 
Sign up to request clarification or add additional context in comments.

5 Comments

For your second solution, couldn't one just count the parameters passed in when invoked?
@Pineda - What do you mean count? Why count them when arguments.length gives you the number directly? I suppose you could iterate over the arguments object to see how many properties it has, but I don't see the point when arguments.length already contains the desired count.
I was referring to the way that the OP was trying to achieve this from outside of the function. I trying to suggest an edit to show the usefulness of the arguments within the function rather than outside of it.
'There are two separate things going on here with arguments defined and arguments actually passed' - that's why we have different terms for them. 'arguments actually passed' are arguments, and 'arguments defined' are parameters.
@estus - I updated my answer to use the proper terms.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.