A colleague and I recently came across code that used the
.length property on a function. This got us asking what this value actually referred to, so I decided to look up the fantastic MDN web docs for some info.
What does it do?
But wait, there’s a catch
This number excludes the rest parameters and only includes parameters before the first one with a default value — MDN
It turns out that rest parameters as well as defaulted parameters do not count towards this number. But what is more interesting is that as soon as a parameter is defined to have a default value within a function, the
function.length property only returns the count of parameters that appear before that defaulted parameter, i.e. all following parameters do not count towards the expected arguments. So we see the behaviour below 🤯
Should I use this?
The behaviour outlined above makes it a fairly unpredictable indication of the number of parameters a function can have. The original code this was seen in was implementing a curry function, however as we have seen, the
function.length would cause errors if any parameter was to be defaulted. Still an interesting API to know and understand nonetheless 🙂