JavaScript Error Handling

JavaScript Error Handling – ReferenceError: assignment to undeclared variable “x”

Today as we progress through our JavaScript Error Handling series we’re going to take a closer look at the Assignment to Undeclared Variable error in all its magnificent glory. The Assignment to Undeclared Variable error crops up anytime code attempts to assign a value to a variable that has yet to be declared (via the var keyword).

Below we’ll go over a few code examples that will illustrate reproduction of a typical Assignment to Undeclared Variableerror, as well as outline how to handle this error when it comes about. Let’s get to it!

The Technical Rundown

  • All JavaScript error objects are descendants of the Error object, or an inherited object therein.
  • The ReferenceError object is inherited from the Error object.
  • The Assignment to Undeclared Variable error is a specific type of ReferenceError object.

When Should You Use It?

As mentioned in the introduction, an Assignment to Undeclared Variable error will occur when attempting to assign a value to a variable which has not previously been declared using the var keyword. However, beyond this cause, there is also one strict limitation within JavaScript itself, which can cause Assignment to Undeclared Variable errors to be suppressed in many circumstances: An Assignment to Undeclared Variable error will only fire when Strict Mode is active within the executing JavaScript.

When Strict Mode is activated, it is a way to programmatically opt in to a restricted variant of JavaScript that has intentionally different semantics and behaviors in a number of cases. While the entirety of Strict Mode is out of the scope of this article, for the purposes of analyzing errors like the Assignment to Undeclared Variable error, it’s critical to understand that Strict Mode causes JavaScript to disallow the creation of accidental global variables.

For example, in normal JavaScript, the following code would be allowed, as the JavaScript engine assumes the intention is to create a new global variable, names, and assign it to the value of splitting the full name string of “Bob Smith” that was provided:

Sure enough, in normal JavaScript, this executes properly without any thrown errors and gives us the expected output:

Therefore, in order for JavaScript to assume it is not the intention of the developer and the code to automatically generate a new global variable anytime an assignment is made to a previously undeclared variable, Strict Mode must be enabled. This is done by including the line ‘use strict’; somewhere in the code, preferably at the same scope as the checks for undeclared assignments.

In this example, we are expanding a bit on the concept above, by creating a simple getFirstName() function, which takes a single full_name parameter, enters into Strict Mode, splits the full_name, and returns the first value (i.e. the first name). By assigning Strict Mode within the function only, we ensure our strict behavior is only within the scope we want, rather than globally across our entire script:

Now that Strict Mode is enabled, the line that worked previously and created a new names global variable now, instead, produces an Assignment to Undeclared Variable error as expected. The specific error message produced will differ slightly depending on the JavaScript engine, but the idea is the same:

The obvious solution to our issue is to ensure that anytime a variable assignment is made, it must already be declared with proper scope, or we must always specify this new variable declaration with the var keyword preceding it:

With no Assignment to Undeclared Variable error produced, as expected, we get our first name output:

These built-in exception classes are helpful JavaScript error handling tools! Used with Airbrake’s JavaScript Error Handler your debugging process will be a breeze. Good luck!