
Return the Length of Arguments Passed in JavaScript

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: 3The 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 one‑liner 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 built‑in arguments object:
function countArguments() { return arguments.length;}It is unlikely that you would find yourself in that sort of compatibility‑constricted 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 Array‑like
Although it behaves like an array, it's actually an Array‑like object which means that it has a length property (which is handy for this particular exercise), but doesn't have Array's built‑in 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 locally‑scoped variable that's only available within non‑arrow type functions, so you can't combine the two examples I've shared above or use it within an arrow.
Wrapping‑Up
Although the real‑world 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:
Related Articles

Dynamic Array Manipulation with JavaScript's splice(). Dynamic Array Manipulation with JavaScript's

The arguments Object vs. Rest Parameters in JavaScript. The
argumentsObject vs. Rest Parameters in JavaScript
Removing p Tags from Contentful List Items. Removing
pTags from Contentful List Items
LeetCode: Finding the Diameter of a Binary Tree. LeetCode: Finding the Diameter of a Binary Tree

Rendering Contentful Rich Code Snippets in Gatsby. Rendering Contentful Rich Code Snippets in Gatsby

JavaScript String Manipulation: substring() vs. substr(). JavaScript String Manipulation:
substring()vs.substr()
Object Control in JavaScript: defineProperties(). Object Control in JavaScript:
defineProperties()
Implementing Server‑Side Rendering (SSR) in Vue. Implementing Server‑Side Rendering (SSR) in Vue

LeetCode: Solving the 'Merge Two Sorted Lists' Problem. LeetCode: Solving the 'Merge Two Sorted Lists' Problem

Lifting State up in React. Lifting State up in React

Understanding the Difference Between 'Indexes' and 'Indices'. Understanding the Difference Between 'Indexes' and 'Indices'

How to Amend Git Commits. How to Amend Git Commits