Using JSLint to Spot JavaScript Issues Early

JSLint will hurt your feelings.

Douglas Crockford

JSLint is a tool developed by Douglas Crockford[2] to compile source JavaScript and inform the user of any potential security and rendering issues that are present in the code. The compiler itself is written in JavaScript, so it’s a fully encapsulated JavaScript environment.

Note

You can access the JSLint tool at http://www.jslint.org. The site lets you select what types of issues you would like to search for and report on.

For instance, take the following JavaScript code, which calculates a timestamp based on a provided year, month, and day:

function toTimestamp(year, month, day){
   var datum = new Date(Date.UTC(year,month-1,day));
   return datum.getTime()/1000
}

tstamp = toTimestamp(2011, 5, 15);

If we run this code block through the JSLint process, we will receive a series of messages listing all of the things that are wrong with our code:

Error:
 Problem at line 3 character 31: Missing semicolon.
 return datum.getTime()/1000
  Implied global: tstamp 6

In this message, we can see that we are missing a semicolon in our function’s return statement. In addition, our tstamp variable is not specified with a var at any point, so it is referenced as an implied global.

Now, if we run the same code through the cajoling process instead, we can see that it reports similar issues:

LOG Checkpoint: InlineCssImportsStage at T+0.548912216 seconds: LOG timestamp.html:4+31: Semicolon inserted:history.html:4: return datum.getTime()/1000 ...

Get Programming Social Applications now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.