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

javascript - Ternary operators returning "true : false". Why? - Stack Overflow

programmeradmin4浏览0评论

Coworker had me grep for the string "true : false" through our project, and I found a lot of ternary operators returning explicit true or false. For example:

return this.state == Cursor.CLOSED ? true : false;

Not just in our project, but plenty of modules/libraries do this. It seems redundant to me, since the author could have just written it like this:

return this.state == Cursor.CLOSED;

Is it defensive coding against some gotcha in Javascript? Or just to be explicit with what you are returning?

Coworker had me grep for the string "true : false" through our project, and I found a lot of ternary operators returning explicit true or false. For example:

return this.state == Cursor.CLOSED ? true : false;

Not just in our project, but plenty of modules/libraries do this. It seems redundant to me, since the author could have just written it like this:

return this.state == Cursor.CLOSED;

Is it defensive coding against some gotcha in Javascript? Or just to be explicit with what you are returning?

Share Improve this question edited Jun 12, 2014 at 15:56 p.s.w.g 149k31 gold badges304 silver badges336 bronze badges asked Jun 12, 2014 at 15:50 Ross HettelRoss Hettel 1,1291 gold badge9 silver badges19 bronze badges 12
  • 3 that seems to simply return true if the state is closed? What's wrong with that? Your question is very unclear. – Liam Commented Jun 12, 2014 at 15:52
  • 3 I think he's asking that it could just be return this.state == Cursor.CLOSED; – Vic Commented Jun 12, 2014 at 15:54
  • 1 To me it seems uneccessary, if this.state == Cursor.CLOSED it's already true ? – adeneo Commented Jun 12, 2014 at 15:54
  • In this case, I'd guess the author didn't realize that he / she could just return the result of the parison. However, return foo || bar ? true : false; is very different from just return foo || bar. – p.s.w.g Commented Jun 12, 2014 at 15:54
  • 2 @RossHettel: In that case, it's being used as a more prominent version of !!. The value wasn't already a boolean — String.prototype.match returns an array. – Chuck Commented Jun 12, 2014 at 16:11
 |  Show 7 more ments

2 Answers 2

Reset to default 11

Is it defensive coding against some gotcha in Javascript?

No. == does always return a boolean value.

The conditional is pletely redundant, and considered a bad practise. Simplify it!

It's quite unnecessary, but it's a pretty mon mistake† in many languages. Because the equality operator is used almost exclusively in conditions, some less-knowledgeable programmers don't know and more experienced programmers occasionally forget that it can actually be used for its value. There has never been any major JavaScript implementation that had a quirk that made this necessary.

† "Mistake" feels unkind here, since the code is correct, just needlessly verbose. But I think you know what I mean.

发布评论

评论列表(0)

  1. 暂无评论