Search code examples
javascriptternary-operatorjslint

JSLint warns about ternary operator


I have the following code in JavaScript:

var a = num ? 5 : "five";

Code seems to be workable. But JSLint warns like this:

#2 Expected '?' at column 9, not column 15.
var a = h ? 5 : "qwerty"; // Line 10, Pos 15  
#3 Expected ':' at column 9, not column 19.
var a = h ? 5 : "qwerty"; // Line 10, Pos 19

So what the problem is? How to disable such warnings?


Solution

  • Its opinion is that:

    The ternary operator can be visually confusing, so ? question mark and : colon always begin a line and increase the indentation by 4 spaces.

    var a = h
        ? 5
        : "qwerty";
    

    To fix either comply with the rule or tick messy whitespace.