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

I am refactoring a bunch of old code and see that JSCS shows a warning about implicit type conversion f

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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744399767a4572327.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信