Use of JSLint to Verify JavaScript

Why is it needed?

Just as C was once a young programming language with primitive compiler support (giving rise to the development of an accessory program called lint to scan a source file looking for problems), JavaScript today is a “young-for-its-age” language with relatively limited developer support.

JavaScript is one of the most widely used and, at the same time, most misunderstood programming languages. It can be amazingly versatile and elegant OR extremely inefficient and sloppy, depending on how YOU use it. JavaScript is very flexible, but, improperly used, this very strength can turn out to be a crippling limitation. JSLint can help you significantly improve the quality of your code and avoid most of the slop.

How to use it?

The simplest way would be to navigate to http://www.jslint.com/, paste your JavaScript into the textarea window, optionally check some configuration setting checkboxes, and click JSLint button.

What are its features?

JSLint is highly configurable. If configured so, it can help you detect the following:

  • Incorrect syntax
  • Undefined variables and functions
  • Improperly spelled property names
  • Missing semicolons
  • Sloppy line breaking
  • Extra commas
  • Missing parentheses
  • Required and forbidden blocks
  • Erroneous expression statements
  • Unfiltered for-in
  • Unintended switch-case fall-through
  • Incorrect use of var
  • Assignment statement in the condition part
  • Type coercion due to == and !=
  • Incorrect use of labels
  • Unreachable code
  • Confusing pluses and minuses
  • Presence of with statement, ++/–, bitwise operators, eval, void
  • Inefficient regular expressions
  • Unsafe characters
  • Incorrect constructors and new usage
  • Etc.

NOTE: The presence of some of the above is valid JavaScript, but goes against the principles of code quality and maintainability.

Notes:

  1. JSLint can be found at http://www.jslint.com/ and its documentation, athttp://www.jslint.com/lint.html.
  2. JSLint can operate on JavaScript source, HTML source, or JSON  text. (HTML: JSLint can inspect the JavaScript contained within … tags. It also scans the HTML content, looking for problems that are known to interfere with JavaScript.)
  3. Your JavaScript is not sent over the network for analysis. JSLint’s analysis is done by a script running on your machine.
  4. JSLint is also available as a widget (http://www.widgetgallery.com/?search=jslint) and in command-line versions.
  5. Criticism: http://dean.edwards.name/weblog/2006/06/jslint/.
  6. JSLint’s usefulness as a helper tool would be directly proportional to your understanding and correct use of its configuration options (provided as checkboxes). Should you decide to use it, please learn to use it well, otherwise the real issues in your code will be buried in a pile of unimportant ones in JSLint output. All in all, know what you are doing, know which options to turn on and which ones to turn off.
150 150 Burnignorance | Where Minds Meet And Sparks Fly!