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

javascript - Is it 100% correct to replace !!someVar with Boolean(someVar)? - Stack Overflow

programmeradmin4浏览0评论

I am refactoring a bunch of old code and see that JSCS shows a warning about implicit type conversion for !!someVar statements.

Is it correct to replace all these implicit conversions with Boolean(someVar) so that nothing breaks due to this change?

I am refactoring a bunch of old code and see that JSCS shows a warning about implicit type conversion for !!someVar statements.

Is it correct to replace all these implicit conversions with Boolean(someVar) so that nothing breaks due to this change?

Share Improve this question asked Jul 26, 2016 at 15:00 Sergei BasharovSergei Basharov 54k78 gold badges207 silver badges352 bronze badges 10
  • 1 Doing the same in disguise doesn't strike as a good practice. Disabling that warning would be cleaner IMO. But your question isn't about opinion, I guess, and it's not clear to me. Are you asking whether the documentation you link to is faulty ? – Denys Séguret Commented Jul 26, 2016 at 15:01
  • 3 No. The global Boolean variable might be overwritten/shadowed, the !! cannot. – Bergi Commented Jul 26, 2016 at 15:03
  • Some linters look like they try too hard to justify their existence. Those "implicit type conversions" are standard, efficient and explicit enough practices. – Denys Séguret Commented Jul 26, 2016 at 15:04
  • @Bergi Can't pretty much any code be broken by overwriting and shadowing? – Katana314 Commented Jul 26, 2016 at 15:06
  • 1 While static analysis tools are immensely valuable, some of their rules tend to be paranoid. Turning those off (I prefer to do so at the project level) is not a bad idea. – ssube Commented Jul 26, 2016 at 15:07
 |  Show 5 more ments

2 Answers 2

Reset to default 12

Is it 100% correct to replace !!someVar with Boolean(someVar)?

No. The global Boolean variable might be overwritten/shadowed, the !! operators cannot.

Is it correct to replace all these implicit conversions with Boolean(someVar) so that nothing breaks due to this change?

Yes. If your code breaks because of this change, it should be considered already broken, and you should fix the thing that messes with Boolean instead.

As another option, you could disable that warning. Using truthiness/falsiness is pretty accepted in Javascript programming. Looks like you can just disable it for boolean and not the other types.

发布评论

评论列表(0)

  1. 暂无评论