Should I use JSLint or JSHint JavaScript validation? [closed]

TL;DR

Use JSLint if you’re looking for a very high standard for yourself or your team, but bear in mind that it’s not necessarily the standard, only a standard, some of which comes to us dogmatically from Doug Crockford.

If you want to be a bit more flexible or have some old pros on your team that don’t buy into JSLint’s opinions or are going back and forth between JavaScript and other C-family languages regularly, try JSHint.

Full version

Two articles with the reasoning behind the fork explain why JSHint exists:

  1. JSHint: A Community-Driven Fork Of JSLint

  2. Why I forked JSLint to JSHint

The idea behind JSLint is that it’s community-driven rather than Crockford-driven. JSHint is generally more lenient (or at least configurable or agnostic) on a few stylistic and minor syntactical opinions that JSLint is a stickler for.

For example, if you think both 1. and 2. below are fine, or if you want to write code with one or more of 1.‘s aspects that aren’t available in 2., JSHint is for you. If you think 2. is the only correct option, use JSLint. I’m sure there are other differences, but this highlights a few.

  1. Passes JSHint out of the box – fails JSLint

    (function() {
      "use strict";
      var x=0, y=2;
      function add(val1, val2){
        return val1 + val2;
      }
      var z;
      for (var i=0; i<2; i++){
        z = add(y, x+i);
      }
    })();
    
  2. Passes Both JSHint and JSLint

    (function () {
        "use strict";
        var x = 0, y = 2, i, z;
        function add(val1, val2) {
           return val1 + val2;
        }
        for (i = 0; i < 2; i += 1) {
            z = add(y, x + i);
        }
    }());
    

I find the JSLint code more visually appealing. The only features of it that I disagree with are its hatred of more than one var declaration in a function and of for-loop var i = 0 declarations, and some of the whitespace enforcements for function declarations.

A few of the whitespace things that JSLint enforces are not necessarily bad but are just out of sync with some pretty standard whitespace conventions for other languages in the family (C, Java, Python, etc.) often followed as conventions in Javascript as well. Since I’m writing in various of these languages throughout the day and working with team members who don’t like Lint-style whitespace in our code, I find JSHint to be a good balance. It catches legitimate bugs and very badly formed code, but doesn’t bark at me like JSLint does (sometimes, in ways I can’t disable) for the stylistic opinions or syntactic nitpicks that I don’t care for.

A lot of good libraries aren’t Lint’able, which to me demonstrates that there’s some truth to the idea that some of JSLint is just about pushing one version of “good code” (which is, indeed, good code). But then again, the same libraries (or other good ones) probably aren’t Hint’able either, so, touché.

Leave a Comment