+ 1
[SOLVED] Why are the outputs appeared different?
If we access any length property of function in Javascript, it shows us the numbers of parameters in the function, right? In my code, a function name getData1 where I use the (a, b, and ...rest) parameters, and a rest operator to collect parameters and in another function getData2 I assign a value to the parameters ( a, b=2, c). But why the output is different? Why length property doesn't show the exact numbers of the parameters? The output would be 3 for both, isn't it? https://sololearn.com/compiler-playground/c1dVKxklFQHt/?ref=app
4 RĂ©ponses
+ 5
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/length
it does not include rest params and stops at the first param with default argument
+ 3
right.
I feel arguments.length seems more useful,
but it is only available inside the function.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/arguments/length
+ 2
Bob_Li That means after setting default value, the other parameters are not counted in second case, right?
+ 1
Bob_Li Thanks for the clarification. That's what I think so.
Yes, this is only available inside the function and create a lot of anomalies!