What is function.length?

Jishaal Kalyan
Sep 19, 2018 · 2 min read

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?

I discovered that function.length is similar to arguments.length but it gives you back the number of arguments a function expects, rather than what is actually passed in.

But wait, there’s a catch

Like almost anything in JavaScript, there is always some quirky behaviour that one needs to be aware of 😓

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 🙂

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade