Variable {a} was not declared correctly


This warning has existed in two forms across the three main linters. It was introduced in the original version of JSLint and has remained in all three tools ever since.

  • In JSLint and JSHint prior to version 2.1.4 the warning given is "Variable {a} was not declared correctly"

  • In JSHint 2.1.4 and above the message used is "You might be leaking a variable ({a}) here"

  • ESLint doesn't support this functionality in the same way but will raise the more generic "'{a}' is not defined" error in the same situation

The situations that produce the warning have not changed despite changes to the text of the warning itself.

When do I get this error?

The "Variable {a} was not declared correctly error (and the alternative "You might be leaking a variable ({a}) here" error) is thrown when JSLint and JSHint encounter more than one inline assignment. In this example, we attempt to assign a string literal to the variables x, y and z:

var x = y = z = "example";

Why do I get this error?

This error is raised to highlight a potential misunderstanding of the language. A relatively common beginner mistake is to use the above code in an attempt to declare multiple variables and assign a single value to all of them at the same time. However, the above is actually equivalent to the following:

var x;
x = y = z = "example";

This makes the problem more obvious. Instead of declaring three variables, we have actually only declared one. y and z will refer to variables with those identifiers in ancestor scopes, or, assuming the code is not running in strict mode, will be created as properties of the global object. If you intended to declare multiple variables, you can use commas to separate them instead:

var x, y, z;
x = y = z = "example";

In JSHint 1.0.0 and above you have the ability to ignore any warning with a special option syntax. The identifier of this warning is W120. This means you can tell JSHint to not issue this warning with the /*jshint -W120 */ directive.

About the author

James Allardice

This article was written by James Allardice, Software engineer at Tesco and orangejellyfish in London. Passionate about React, Node and writing clean and maintainable JavaScript. Uses linters (currently ESLint) every day to help achieve this.