1

I am looking at a piece of code which detects IE browser:

if (false || !!document.documentMode)

and I am not understanding the contraption. Why is it necessary to OR with false and use NOT twice?

If I simply loaded below file in IE9, FF or Opera, then IE would tell me that document mode was there, while the later two would say otherwise:

<html>
<head>
    <script>function ld() {
        if (document.documentMode){
            document.getElementById("p1").innerHTML = 'Document Mode detected'
        }
        else {
            document.getElementById("p1").innerHTML = 'No Document Mode'
        }
    }</script>
</head>
<body onload="ld()">
<p id="p1"></p>
</body>
</html>

Is that not sufficient and why? It is not clear, because if I replaced the condition with the one in my original question, the result would be exactly the same. What am I missing?

ajeh
  • 2,652
  • 2
  • 34
  • 65
  • Please change your title please to something more professional. – kemicofa ghost Apr 09 '15 at 21:10
  • Where did you find the code? – Anonymous Apr 09 '15 at 21:13
  • 1
    1) "*Why is it necessary to OR with false [...]*" It isn't. Best guess is that it's out of a habit of needing a boolean value for the `if` condition, which JavaScript doesn't require. 2) "*[...] and use NOT twice?*" [The operators convert the value to a boolean](http://stackoverflow.com/questions/2174297/how-to-use-the-double-not-operator-in-javascript), same as `Boolean(document.documentMode)`. Though, after removing the `false ||`, the `if` will do the boolean conversion itself, so they're also unnecessary. – Jonathan Lonowski Apr 09 '15 at 21:19
  • @JonathanLonowski Just what I thought, but overcomplication simply stunned me and the feeling was that I missed some secret wisdom. This would make a good answer. – ajeh Apr 09 '15 at 21:25

2 Answers2

2

Why is it necessary to OR with false [...]

It isn't necessary. The || operator, given false for the 1st operand, will always return the 2nd operand.

// lval || rval (minus short-circuiting)
function OR(lval, rval) {
    if (lval)
        return lval;
    else
        return rval;
}

OR(false, 'foo') // 'foo'

[...] and use NOT twice?

This part already has an answer here on SO.

Two ! operators together perform a "ToBoolean" type conversion, as a slighter shorter version of using Boolean() without new:

!!document.documentMode        // true/false
Boolean(document.documentMode) // true/false

Also, the if will perform the same type conversion itself.

2. If ToBoolean(GetValue(exprRef)) is true

So, when testing a single value for truthiness, the !! aren't necessarily either, as you suggested:

if (document.documentMode)
Community
  • 1
  • 1
Jonathan Lonowski
  • 121,453
  • 34
  • 200
  • 199
1

Since document is always defined, and the presence of its property documentMode is truthy, these are completely synonymous:

if (false || !!document.documentMode)

and:

if(document.documentMode)

(If document was possibly undefined, then the first code would fail altogether.)

Rick Hitchcock
  • 35,202
  • 5
  • 48
  • 79