3

see http://webmail.mac.com source.

  B.incompatible = !!(B.msie < 7 || B.safari < 500 || B.mozilla < 1.008000999);
  B.unsupported  = !!(B.opera || B.safari < 500) && !B.incompatible;
  B.supported    = !B.incompatible && !B.unsupported;

Why are the double "!" used here? Is there any benefit?

Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
Cynial
  • 680
  • 8
  • 23
  • It coerces a value to a Boolean. – Aadit M Shah Nov 09 '11 at 10:30
  • 2
    No time to search? Or to look at the top of the "related" list that handily came up whilst you were writing your question, trying to stop you from posting this pointless duplicate? – Lightness Races in Orbit Nov 09 '11 at 10:31
  • see http://stackoverflow.com/questions/784929/what-is-the-operator-in-javascript – Irishka Nov 09 '11 at 10:32
  • In this case, surely it would be a bool anyway? It's performing || and && on a set of comparison operators. So the question is still valid. What benefit is it providing *here*? – deworde Nov 09 '11 at 10:34
  • Admittedly this q might be more suitable on Code Review, rather than StackOverflow. – deworde Nov 09 '11 at 10:34
  • I do not think this is a duplicate? The linked question asks what does the operator do while in this question it asks what the benefit is, which is a good question since most of the time when I see !!, there has been no point of doing it at all. – Esailija Nov 09 '11 at 10:37
  • @Esailija: I think the problem was the poorly chosen title *What's double “!” mean in JS?* which might have caused some people (including me admittedly) to close the question as duplicate too quickly. – Felix Kling Nov 09 '11 at 10:39
  • @FelixKling, should it be retitled and reopened then? I am curious to know if there is a real world situation where there is an actual functional benefit of doing this. – Esailija Nov 09 '11 at 10:43
  • @Esailija thanks. I just want to know "why". – Cynial Nov 09 '11 at 10:44
  • @FelixKling can I change my title? – Cynial Nov 09 '11 at 10:44
  • @Esailija: Probably, but I'm afraid there is not a satisfying answer to this question other than that it serves no purpose here (which would still be a valid answer). Cynial: You should be able to change your title when you edit your question, but I can do it for you. I voted to reopen anyway. – Felix Kling Nov 09 '11 at 10:47
  • @Felix Kling I know and I think "ask first" is a courteous way when the q be closed. – Cynial Nov 09 '11 at 10:54
  • Actually, there is a really good reason to use the Boolean coercion operator ("!!"). You see the logical AND ("&&") and OR ("||") operators don't explicitly return Boolean values. If the first operand evaluates to true, AND returns the second operand and OR returns the first operand. Opposite for false. Hence they are used as guard and default operators. – Aadit M Shah Nov 09 '11 at 13:18
  • In the aforementioned code, the author explicitly wanted the properties incompatible, unsupported, and supported to be Boolean values. Hence the Boolean coercion operator. – Aadit M Shah Nov 09 '11 at 13:21

3 Answers3

4

There is no point in this case, since the expressions already evaluate to boolean values anyway. It's probably just a programmer being "extra sure".

Matthew Crumley
  • 101,441
  • 24
  • 103
  • 129
  • Hmm, I missed `B.opera`, but even in that case, the whole expression would still evaluate to a boolean (because of its structure, not because of any language feature that coerces it implicitly). – Matthew Crumley Nov 09 '11 at 14:20
  • yep. IMO, there's rarely a use case for `!!`. The code in question is probably the worst use of it I've ever seen. – Andy E Nov 09 '11 at 14:24
2

The logical NOT operator ("!") is used to convert true to false and vice versa.

! true // expresses false
! false // expresses true

However, it also coerces values. Non zero numbers and non empty strings are true. So they become false.

! 1.0 // expresses false
! 0.0 // expresses true
! '@' // expresses false
! '' // expresses true

Using two NOTs converts it back to the original Boolean value.

!! 1.0 // expresses true
!! 0.0 // expresses false
!! '@' // expresses true
!! '' // expresses false

It's equivalent to calling the Boolean constructor. However, it's faster (no function call overhead), shorter, and more readable.

!! 1.0 === Boolean(1.0) // expresses true
Aadit M Shah
  • 72,912
  • 30
  • 168
  • 299
0

Its a very smart way of casting to a bool.

TJHeuvel
  • 12,403
  • 4
  • 37
  • 46