最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - JSLint warns about ternary operator - Stack Overflow

programmeradmin0浏览0评论

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?

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?

Share Improve this question asked Jan 28, 2016 at 16:36 fbjornfbjorn 9001 gold badge14 silver badges30 bronze badges 3
  • 2 Is your indentation correct? Found this online: "With JSLint you'll get an "Expected '{a}' at column {b}, not column {c}" error any time an incorrect indentation width is found." – IrkenInvader Commented Jan 28, 2016 at 16:39
  • Seems like you should put more code in here, currently, your ? is at column 11. – Loïc Faure-Lacroix Commented Jan 28, 2016 at 16:41
  • When I place it to begin of the line: #2 Undeclared 'h'. var a = h ? 5 : "qwerty"; // Line 4, Pos 9 #3 Expected '?' at column 5, not column 11. var a = h ? 5 : "qwerty"; // Line 4, Pos 11 Indentation is 1:1 like in question. – fbjorn Commented Jan 28, 2016 at 16:44
Add a ment  | 

1 Answer 1

Reset to default 11

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 ply with the rule or tick messy whitespace.

发布评论

评论列表(0)

  1. 暂无评论