Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 792
fbjorn Avatar asked Jan 28 '16 16:01

fbjorn


1 Answers

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.

like image 116
Alex K. Avatar answered Sep 23 '22 17:09

Alex K.