开发者

More elegant alternative for "value === false"?

In JavaScript, I want to compare a value for (strict) equality to false.

From Java / C# I am used to writing:

if (!value)
   //Do Something

However, I can not use it in JavaScript as null, undefined (and others IMHO) evaluate to false inside an if-statement, too. (I don't want that).

Thus开发者_如何学Go, I have therefore been writing the following to formulate such a check:

if (value === false)
   //Do Something

Yet, this construct looks a little bit strange to me.

Are there any more elegant ways here (which lead to the same results as the === false of course)?

Introducing a method isFalse would be an option, of course, but that's not what I am looking for as it would look even more distracting than the === false.


If you truly want to check for an explicit false value in Javascript, as opposed to a 'falsy' value (null, undefined, 0, etc.) then === false is by far the most standard way of doing so. I don't think it needs to be changed - that's part of the beauty of such a dynamic language!


Please, don't write a function isFalse() (a candidate for The Daily WTF), use the === operator even if it looks strange to you, there is no cleaner way.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜