Twitter GitHub Facebook Instagram dirv.me

Daniel Irvine on building software

Prefer JSLint over JSHint

17 July 2014

When linting JavaScript code, you have a choice between JSLint and JSHint. JSHint is a fork of JSLint. It was forked because of a belief that the author, Douglas Crockford, is being too strict. JSHint is more lenient.

Part of the problem is that some of the suggestions that JSLint makes are patently ridiculous in--hmmm... how do I put this?--more... traditional languages. For example, this code snippet is incorrect according to JSLint:

for(var i = 0; i < 10; ++i) {
}

It should in fact be written like this:

// Beginning of method
var i;
// ...

for(i = 0; i < 10; ++i) {
}

The first version has a scoping issue; the variable i is defined at global scope, rather than block-level as it would be in every other language on the planet. This introduces a subtle scoping issue that can and will bite you in the ass.

But to rewrite the loop in the second form is craziness and most sane people, like the authors of JSHint, will look at this and recoil in horror. That’s not the way we’ve been taught to write code, and you can’t force us to do it! It doesn’t look right; it’s jarring.

The unfortunate truth is that the first example is just not safe. By writing code the first way, you are increasing the risk of erroneous behaviour in your program. And if that happens, you will undoubtedly spend hours if not days tracking down that bug.

Use JSLint for your own safety. It will help to ensure you write bug-free JavaScript code.

About the author

Daniel Irvine is a software craftsman at 8th Light, based in London. These days he prefers to code in Clojure and Ruby, despite having been a C++ and C# developer for the majority of his career.

For a longer bio please see danielirvine.com. To contact Daniel, send a tweet to @d_ir or use the comments section below.

Twitter GitHub Facebook Instagram dirv.me