1

I understand the basic concept of double-negation- conversion to bool - this is a question about the specific use before new.

I was looking for a way to detect blob support and came across this check on this page:

try { !!new Blob(); } catch (e) { return false; }

I created an example below to demonstrate the check always failing.

window.onload=function()
{
  var inputBox = document.getElementById('inputBox');
  try {
    !!new Foo15514();
    inputBox.value='supported';
  } catch (e) {
    inputBox.value='not supported';    
  }
}
<input id='inputBox' type=text/>

Without getting into whether this is a good approach for blob detection or not, the question I have is what is the point of the !! in this case? As far as I can tell it is superfluous, but I thought I would ask in case there is something I am missing.

!!new Foo15514(), new Foo15514(), var j = new Foo15514() all have the same result.

Update Just to get the ball rolling - one thought I had is that this was done to force the javascript engine to evaluate this rather than skipping it since it has no effect, which seems like a bad approach if that was the case.

acarlon
  • 16,764
  • 7
  • 75
  • 94
  • @michaelpri did you read the first sentence? I understand `!!`. This is a specific question about use before new in the sample shown. – acarlon May 14 '15 at 02:59
  • I dont think it is a duplicate. OP clearly states he knows what it is but is asking about its purpose/ necessity in this particular case – Dave Pile May 14 '15 at 03:00
  • !! does nothing here as the `Foo1234()` call triggers an exception before it can be used – Patrick Evans May 14 '15 at 03:04
  • @PatrickEvans - thanks for confirming. Possibly the snippet in the reference was just taken out of context. – acarlon May 14 '15 at 03:07

1 Answers1

2

In this case, it is indeed superfluous. Using the ! operator twice just casts a value to boolean.

However, the code you read in that bug report is not complete (like a typo). It omitted an important part of what was actually meant. If you check the commit that was made, the picture looks different:

function test() {
    try {
        return !!new Blob();
    } catch (e) {
        return false;
    }
}
Community
  • 1
  • 1
Bergi
  • 630,263
  • 148
  • 957
  • 1,375