JSLint Messages – Coding Convention and Style Guide

Why Bother?

JSLint contains a number of messages pertaining to the style, appearance and formatting of JavaScript code. This includes stipulations about bracing positions, indentation and spacing.

As a demonstration of this paste the following into http://jslint.com/

var x = 0;
switch(x){
    case 1:
        x++;
        break;
    case 2:
        x--;
        break;
    default:
        break;
}

Zero errors, right? Now click the flag ‘Strict white space’ (‘white’) and recheck…

Yikes! 10 errors (at the time of writing)!

So why should JSLint possibly care what your code looks like? After all, it’s all about bugs, isn’t it?

Well, on the surface, legible code equates to more maintainable code, which itself equates less ambiguity and fewer bugs. But you may have your own internal guidelines that dictate how the JavaScript looks, i.e. how you indent, where you put your braces, etc. So you will have probably flipped the ‘white’ flag to ‘off’. After all, when given the above code snippet, JSLint returns a substantial amount of what you consider to be ‘noise’, which if no use to you. Right?

However in turning this option off, you are missing the opportunity to have a uniform and consistent convention across your code base, and a tool that can tell you where you are deviating from it.

But sometimes, formatting and positioning does have functional implication as well, and can lead itself to bugs. Consider the following code:

function GetObjectLiteral()
{
    return
   {
       x:0,
       y:1
   };
}
alert(GetObjectLiteral().x); //what do you expect?

Regardless of what you expect the above to return, what you will not get is an alert box with the text ‘0’. Different browsers may behave differently. IE8 complains about a missing semi-colon, whilst others will simply execute the ‘return’, passing control back to the calling function, and then fail to find the member ‘a’

Changing the bracing position fixes the code:

function GetObjectLiteral(){
    return{
       x:0,
       y:1
   };
}
alert(GetObjectLiteral().x); //alerts '0'

The Messages

So, let’s have a look at some of these messages…

Expected ‘{a}’ at column {b}, not column {c}.

This is a simple case of incorrect indentation. The most basic demonstration of this can be seen in following code snippet:

var a = 0;
  var b = 0; //Problem at line 2 character 3: Expected 'var' at column 1, not column 3

Expected ‘{a}’ to have an indentation of {b} instead of {c}.

This is another indentation catch. The default indentation step is 4, meaning that indentation columns should start at positions 1, 5, 9, 13, 17, etc. The sample below uses an indent of 5 spaces, placing the start of the new column at character 6 (as the message denotes).

function MyFunc() {
     alert("hello"); //Problem at line 2 character 6: Expected 'alert' at column 5, not column 6.
//3456789
}

Expected exactly one space between ‘{a}’ and ‘{b}’.

This message goes some way to supporting JSLint’s assertion of correct bracing positions. You will see this message generated on the following code snippet.

if (x === 0)
{  //brace on next line
    alert("hello");
}

But even when you do place the brace on the correct line, JSLint requires you to apply the correct spacing. So, the following code snippets will also yield this message:

if (x === 0){ //no spaces
    alert("hello");
}

if (x === 0)  {//two spaces
    alert("hello");
}

Missing space between ‘{a}’ and ‘{b}’.

Haveyoutriedreadingsentencesthathavetheirspacesomitted? Generally, it’s not that easy, is it?

if (x === 0){    //Missing space between ')' and '{'
    alert("hello");
}

Mixed spaces and tabs.

This message is displayed when a line is indented with a mixture of spaces and tabs. Most IDEs will have an option to convert tabs into spaces automatically. I suggest you turn this on.

Unexpected space between ‘{a}’ and ‘{b}’

Here, JSLint is reporting that you have included a space where it did not expect one. The following code snippet will yield this message:

if ( x === 0){  //Unexpected space between '(' and 'x'
    alert("hello");
}

A Guide To JSLint Messages

This article is one of a series on the error and warning messages produced by JSLint.

4 thoughts on “JSLint Messages – Coding Convention and Style Guide

  1. I have face problem: Microsoft JScript runtime error: ‘document.getElementById(…)’ is null or not an object in IE8

    function makeOneWay() {
    document.getElementById(‘f1_rdate’).style.visibility = “hidden”;
    document.getElementById(‘ReturnIcon’).style.visibility = “hidden”;
    document.getElementById(‘Return’).style.visibility = “hidden”;
    document.getElementById(‘f1_lblRandOne’).value = “o”;

    }
    function makeRoundTrip() {
    document.getElementById(‘f1_rdate’).style.visibility = “visible”;
    document.getElementById(‘ReturnIcon’).style.visibility = “visible”;
    document.getElementById(‘Return’).style.visibility = “visible”;
    document.getElementById(‘f1_lblRandOne’).value = “r”;

    }
    function showhideint() {
    document.getElementById(‘int’).style.display = “block”;

    document.getElementById(‘dom’).style.display = “none”;
    // // alert();
    document.getElementById(‘int2’).style.display = “block”;
    document.getElementById(‘dom2’).style.display = “none”;
    document.getElementById(‘f1_lblFandD’).value = “i”;
    }
    function showhidedom() {
    document.getElementById(‘int’).style.display = “none”;
    document.getElementById(‘dom’).style.display = “block”;
    document.getElementById(‘int2’).style.display = “none”;
    document.getElementById(‘dom2’).style.display = “block”;
    document.getElementById(‘f1_lblFandD’).value = “d”;
    }

  2. ouch, the code got its spaces stripped, but I think you know what I`m referring to.

  3. Hi James,

    This is an amazing work, thanks for the time and effort!

    I agree with all what you have written but one thing:

    Your first example shows the warning that is most annoying for me, as my editor will always indent the case statements;

    It is not clear about your thoughts regarding the case alignment with switch.

    I think (as you mentioned above) the code below is perfect:


    var x = 0;
    switch(x){
    case 1:
    x++;
    break;
    case 2:
    x--;
    break;
    default:
    break;
    }

    But JSLint says it should be:


    var x = 0;
    switch(x){
    case 1:
    x++;
    break;
    case 2:
    x--;
    break;
    default:
    break;
    }

    Which is personally kinda weird (I’ve never seen this construct in almost 25 years of programming)

    Cheers, and keep it up!
    Luciano

  4. Pingback: JSLint – A Guide To JSLint Messages | James Wiseman

Leave a Reply