Fork me on GitHub

JSLint Error Explanations

JSLint will hurt your feelings. It's time to make them better!


'{a}' was used before it was defined

When do I get this error?

JSLint will throw the "'{a}' was used before it was defined" error when it encounters a reference to an identifier that has not been declared as part of a var or function statement. Some very common examples of this error are those that refer to native DOM objects:

  • "'document' was used before it was defined"
  • "'window' was used before it was defined"
  • "'alert' was used before it was defined"
  • "'console' was used before it was defined"
  • "'require' was used before it was defined" (commonly seen with Node.js)

In the following example we attempt to set the value of the variable x that has not been defined, and then attempt to show the value of that variable in several ways. We also attempt to set the value of y to the return value of a function which has not been defined:

Why do I get this error?

This error is raised to highlight potentially dangerous code or code that could cause a fatal error. Your code may run without error, depending on the identifier in question, but is likely to cause confusion to other developers and could in some cases cause a fatal error that will prevent the rest of your script from executing.

The example above is valid JavaScript when not running in strict mode. It will create a property of the global object (in the browser, the global object is window) with the given identifier. If you had accidentally omitted the var keyword, you could have ended up overwriting a variable declared in a parent scope, causing unexpected behaviour. If it does run in strict mode, it will generate a ReferenceError as it's illegal to assign a value to an undefined variable under such conditions (ES5 Annex C):

Assignment to an undeclared identifier or otherwise unresolvable reference does not create a property in the global object. When a simple assignment occurs within strict mode code, its LeftHandSide must not evaluate to an unresolvable Reference. If it does a ReferenceError exception is thrown.

If you are referring to an identifier that has been declared elsewhere (perhaps in another JavaScript file included in a page), you can tell JSLint about it by using the global directive:

If you have mistakenly omitted a var keyword, you can fix this error by simply adding it in. If you omitted the keyword on purpose (perhaps to allow access to a variable from other scopes), declare the variable in the top-most scope in which it should be available:

If the error is referring to a built-in DOM object, such as alert, document or window, you can set some JSLint directives to make the parser aware of the fact that these built-in objects are actually available. Functions like alert are treated as debugging aids by JSLint. You can use the devel option to prevent warnings concerning these:

Other common browser built-ins can be pre-defined with the use of the browser option:

Finally, common Node.js built-ins can be pre-defined with the use of the node option:


James Allardice

This article was written by James Allardice, an enthusiastic young JavaScript developer at Global Personals (we're looking for developers so please apply). He is passionate about writing clean, maintainable JavaScript and uses JSHint every day to help achieve this.

You can follow him on Twitter, fork him on GitHub and find him on Google+.


Proceeds generated by this site are donated to help advance other open source projects



comments powered by Disqus