[SOLVED] Why are the outputs appeared different? | Sololearn: Learn to code for FREE!
Novo curso! Todo programador deveria aprender IA generativa!
Experimente uma aula grƔtis
+ 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

27th Apr 2024, 9:40 AM
`Š½Ń‚Ń‚pā“ā°ā¶
`Š½Ń‚Ń‚pā“ā°ā¶ - avatar
4 Respostas
+ 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
27th Apr 2024, 11:03 AM
Bob_Li
Bob_Li - avatar
+ 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
27th Apr 2024, 12:16 PM
Bob_Li
Bob_Li - avatar
+ 2
Bob_Li That means after setting default value, the other parameters are not counted in second case, right?
27th Apr 2024, 11:10 AM
`Š½Ń‚Ń‚pā“ā°ā¶
`Š½Ń‚Ń‚pā“ā°ā¶ - avatar
+ 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!
27th Apr 2024, 12:18 PM
`Š½Ń‚Ń‚pā“ā°ā¶
`Š½Ń‚Ń‚pā“ā°ā¶ - avatar