When C was a young programming language, there were several common programming errors
that were not caught by the primitive compilers, so an accessory program called lint was developed that would scan a source file, looking
for problems.
As C matured, the definition of the language was strengthened to eliminate some
insecurities, and compilers got better at issuing warnings. lint is no longer needed.
JavaScript is a young-for-its-age language. It was originally intended to do small
tasks in web pages, tasks for which Java was too heavy and clumsy. But JavaScript is a
very capable language, and it is now being used in larger projects. Many of the features
that were intended to make the language easy to use are troublesome for larger projects.
A lint for JavaScript is needed: JSLint, a JavaScript
syntax checker and verifier.
JSLint is a code quality tool for JavaScript. It takes a source text and scans it. If it finds a problem, it returns a message describing the problem and an approximate location within the source. The problem is not necessarily a syntax error, although it often is. JSLint looks at some style conventions as well as structural problems. It does not prove that your program is correct. It just provides another set of eyes to help spot problems.
JSLint defines a professional subset of JavaScript, a stricter language than that defined by the third edition of the ECMAScript Language Specification. The subset is closely related to the style recommendations from Chapter 9.
JavaScript is a sloppy language, but inside it there is an elegant, better language. JSLint helps you to program in that better language and to avoid most of the slop.
JSLint can be found at http://www.JSLint.com/.
JavaScript's biggest problem is its dependence on global variables, particularly
implied global variables. If a variable is not explicitly declared (usually with the
var statement), then JavaScript assumes that
the variable was global. This can mask misspelled names and other
problems.
JSLint expects that all variables and functions will be declared before they are used or invoked. This allows it to detect implied global variables. It is also good practice because it makes programs easier to read.
Sometimes a file is dependent on global variables and functions that are defined elsewhere. You can identify these to JSLint by including a comment in your file that lists the global functions and objects that your program depends on, but that are not defined in your program or script file.
A global declaration comment can be used to list all of the names that you are
intentionally using as global variables. JSLint can use this information to identify
misspellings and forgotten var declarations. A
global declaration can look like this:
/*global getElementByAttribute, breakCycles, hanoi */
A global declaration starts with /*global.
Notice that there is no space before the g. You
can have as many /*global comments as you like.
They must appear before the use of the variables they specify.
Some globals can be predefined for you (see the later section "Options"). Select the "Assume a browser" (browser) option to predefine the standard global properties that are
supplied by web browsers, such as window and
document and alert. Select the "Assume Rhino" (rhino) option to predefine the global properties provided by the
Rhino environment. Select the "Assume a Yahoo Widget" (widget) option to predefine the global properties provided by the
Yahoo! Widgets environment.