Return the Length of Arguments Passed in JavaScript

Hero image for Return the Length of Arguments Passed in JavaScript. Image by Alexis Fauvet.
Hero image for 'Return the Length of Arguments Passed in JavaScript.' Image by Alexis Fauvet.

Although relatively simple, the "Return Length of Arguments Passed" problem involves determining the number of arguments passed into a function and returning that count as a number. I would argue that there it would be rare that you encounter a scenario where you would need to outside of variadic function signatures, but this is exactly the type of exercise you might find on the likes of LeetCode or HackerRank platforms, or even presented to you as part of an interview.


Understanding the Problem

The problem is fairly straightforward: given a function call, how can we efficiently calculate the number of arguments that were provided to that function? For instance, if we have a function countArguments and we call it with varying numbers of arguments, we want to obtain the count of those arguments.

Here are some examples:

countArguments();  // Expected Output: 0countArguments(1, 2, 3);  // Expected Output: 3countArguments('a', 5, true, null);  // Expected Output: 4countArguments({}, ' ', false);  // Expected Output: 3

The Solution in ES6 (and TypeScript)

The most concise solution I can come up with (using TypeScript for the sake of clarity) uses the rest parameter and looks like this:

const countArguments = (...args: any[]): number => args.length;

This is a literal oneliner solution where we use the rest parameter ...args to collect all of the provided arguments into an array, and then return the length of that array.


The Solution Without ES6

If we were to tackle this problem using Python, we could use *args syntax to accept a variable number of arguments and then return their count using len(args). If we wanted to solve the same problem using more traditional JavaScript (and without ES6), we would do something similar, using the builtin arguments object:

function countArguments() {  return arguments.length;}

It is unlikely that you would find yourself in that sort of compatibilityconstricted environment these days, but it's always worth having an alternative to demonstrate, and a little more knowledge of the underlying language's features.

With the arguments object though there are two things you want to understand:

arguments is Arraylike

Although it behaves like an array, it's actually an Arraylike object which means that it has a length property (which is handy for this particular exercise), but doesn't have Array's builtin methods like forEach() or map(). You can still convert it to a 'real' Array by using slice() or Array.from() though.

It isn't Available Inside Arrow Functions

arguments is a locallyscoped variable that's only available within nonarrow type functions, so you can't combine the two examples I've shared above or use it within an arrow.


Wrapping‑Up

Although the realworld cases where you might actually need to count the number of arguments passed into a function is relatively limited, it is also a very easy task to achieve in JavaScript.


Categories:

  1. Guides
  2. JavaScript
  3. LeetCode