JavaScript Error Handling

JavaScript Errors – More Arguments Needed

Next up in our JavaScript Error Handling series is the More Arguments Needed TypeError. The More Arguments Needed TypeError is typically thrown when calling a method or function that requires a minimum number of arguments that simply weren’t provided by the calling code.

In today’s article we’ll examine the More Arguments Needed TypeError in a bit more detail by looking at where it sits within the JavaScript Exception hierarchy. We’ll also use a few code examples to illustrate how More Arguments Needed TypeErrors most commonly occur, so let’s get started!

The Technical Rundown

  • All JavaScript error objects are descendants of the Error object, or an inherited object therein.
  • The TypeError object is inherited from the Error object.
  • The More Arguments Needed TypeError is a descendant of TypeError object.

When Should You Use It?

In this first example we have an array containing some basic info of one of my all-time favorite books. However, we want to split the data up a bit into more manageable sub-arrays: One array with just the title and author and the other with just the publication date.

To accomplish this we’ll use the Array.prototype.filter() method which expects the first argument to be a function that will be executed for every entry in the array. If that function returns a true boolean value the element is added/kept in the array and if it returns false that element is tossed out. Therefore, we’re using the new ES6 arrow function syntax to create an anonymous inline function that will assign our element to the v parameter. We then check that the typeof v is a string to get the author and title, while we verify the instanceof value to be Date to grab our publication date:

Sure enough this works as expected and we get two filtered arrays. However, like many methods in JavaScript the Array.prototype.filter() method expects at least one argument to be passed to it. This makes sense since there’d be no point in looping through every array element and performing a filter that does nothing at all, which is what would happen if we passed zero arguments. Therefore, let’s see what happens if we try this same example but without providing an argument to Array.prototype.filter:

Perhaps unsurprisingly this throws a More Arguments Needed TypeError, although the formatting of the message differs depending on the JavaScript engine (i.e. browser) in use:

There seems to be little rhyme or reason to determine which built-in methods and functions in JavaScript will throw a More Arguments Needed TypeError when not enough arguments are provided, so it’ll require a bit of experimentation to figure out which abide by these requirements and which do not. Obviously the best practice is to try to just write code that doesn’t make improper argument quantity calls in the first place (ESLint and other IDE parsers help a lot in this regard).

Another related and fun consideration when working with JavaScript is to recall that JavaScript doesn’t enforce any sort of maximum number of arguments that can be passed to a method. Instead, JavaScript automatically creates a metaparameter called arguments within the scope of every function/method block. arguments is an Array-like object that contains all the argument values passed to that particular function/method call. This knowledge means that even though many methods have a minimum number of arguments that must be passed there is never a maximum number, which can be used to your advantage in some cool ways.

For example, here we have a little Book class that lets us create book objects more easily. We also have the Book.toArray() method that returns an array of the instance properties, with or without the .publicationDate property, depending on the boolean value of the single withPublicationDate parameter:

As you can see from the output below our calls to the Book.toArray() method work as expected, generating an array we can output with and without publicationDate depending on that first argument. However, let’s try modifying our Book.toArray() method a bit to make use of the hidden arguments parameter:

Now when we call Book.toArray() any and all parameters we provide — even beyond the first withPublicationDate boolean — will be combined into the output of our full array. Sure enough, if we test this out by passing a few extra parameters to our second book.toArray() call we get the extra elements in the returned and output array:

To dive even deeper into understanding how your applications deal with JavaScript Errors, check out the revolutionary Airbrake JavaScript error tracking tool for real-time alerts and instantaneous insight into what went wrong with your JavaScript code.