4

Possible Duplicates:
What does this expression mean “!!”
What does the !! operator (double exclamation point) mean in JavaScript?

Here is a snippet from Prototype Javascript Library :

  Browser: (function(){
    var ua = navigator.userAgent;
    var isOpera = Object.prototype.toString.call(window.opera) == '[object Opera]';
    return {
      IE:             !!window.attachEvent && !isOpera,
      Opera:          isOpera,
      WebKit:         ua.indexOf('AppleWebKit/') > -1,
      Gecko:          ua.indexOf('Gecko') > -1 && ua.indexOf('KHTML') === -1,
      MobileSafari:   /Apple.*Mobile/.test(ua)
    }
  })(),

This is all good and i understand the objective of creating a browser object. One thing that caught my eye and I haven't been able to figure out is the use of double not operator !! in the IE property.

If you read through the code you will find it at many other places. I dont understand whats the difference between !!window.attachEvent and using just window.attachEvent.

Is it just a convention or is there more to it that's not obvious?

Community
  • 1
  • 1
Rajat
  • 32,970
  • 17
  • 67
  • 87

2 Answers2

4

It's a fairly common trick used to coerce a value to a boolean type, instead of using (bool). !window.attachEvent would negate the truth value of window.attachEvent, giving you a boolean; !!window.attachEvent negates that, giving you the original truth value but as a boolean instead of the type of window.attachEvent

Michael Mrozek
  • 169,610
  • 28
  • 168
  • 175
  • imo this is a much better answer then the accepted one (which talks more about && then !!). – Matt Briggs Jun 18 '10 at 03:19
  • @Matt, the real question was "Why use `!!window.attachEvent && !isOpera` instead of just `window.attachEvent && !isOpera`", the OP was not really asking what `!!` does (fact that I think the OP already knows). To understand why you need `!!` in that condition, you need to know how the `&&` operator behaves, this can cause confusion because in other languages the logical operators *always* yield a boolean value. That's why I talk about `&&` ;) – Christian C. Salvadó Jun 18 '10 at 04:16
  • @CMS I think he was just asking what the point of double-negating something was, he said "If you read through the code you will find it at many other places. I dont understand whats the difference between `!!window.attachEvent` and using just `window.attachEvent`"; that his example used `&&` was just a coincidence I think. I found your answer educational though – Michael Mrozek Jun 18 '10 at 04:56
4

I dont understand whats the difference between !!window.attachEvent and using just window.attachEvent.

The key of understanding this, is to know that the Boolean Logical Operators can return an operand, and not a Boolean result necessarily:

The Logical AND operator (&&), will return the value of the second operand if the first is truly:

true && "foo"; // "foo"

And it will return the value of the first operand if it is by itself falsy:

undefined && "anything"; // undefined
NaN && "anything";       // NaN
0 && "anything";         // 0

So, in the snippet !!window.attachEvent && !isOpera, we already know that isOpera is a boolean value, !! will just make sure that Browser.IE is a boolean result also.

An example: let's say we are in Firefox, window.attachEvent is undefined and !isOpera is true, if you don't use the double negation, Browser.IE would be undefined instead false.

Christian C. Salvadó
  • 807,428
  • 183
  • 922
  • 838