4

Note: it's actually a duplicate of What is the difference between if(!!condition) and if(condition)

While I understand what the !! means (double not), for this same reason it doesn't make sense to me its use in the MDN documentation:

if (!!window.Worker) {
...
}

Isn't this exactly the same as this for this situation?

if (window.Worker) {
...
}

The casting to boolean makes no sense for me since the if will only be executed if the window.Worker exists. To say that it's True or Object for an if() conditional (I think) is the same.

So, why is the !! used here? Or, why is the window.Worker casted to boolean inside an if()?

Community
  • 1
  • 1
Francisco Presencia
  • 8,732
  • 6
  • 46
  • 90
  • Please refer to the original one (the linked as a dup is actually a dup): http://stackoverflow.com/questions/19818574/what-is-the-difference-between-ifcondition-and-ifcondition – Francisco Presencia May 18 '15 at 06:19
  • 2
    People who come from typed languages are uncomfortable with truthiness. Therefore you see this sort of code as a sort of "readable code" style documentation that it's checking for the boolean value rather than the object itself. People who're used to javascript programming will typically not do this. – slebetman May 18 '15 at 06:19
  • @slebetman that's why it didn't make sense for me then! Your comment should be the answer, since it not only explains that it's the same, but one possible cause (: – Francisco Presencia May 18 '15 at 06:20
  • 1
    It should be noted that Mozilla, while to us is a software that executes javascript, to the Mozilla devs it is C/C++ code for a web browser. So the documentation are sometimes written by C++ developers rather than javascript developers. Javascript developers wouldn't dream of creating a function called `document.getElementById()`. – slebetman May 18 '15 at 06:22

1 Answers1

2

Yes it is exactly the same. Nothing to add.

It might have been used to emphasize that the window.Worker property - expected to be a function - is cast to a boolean for detecting its presence, instead of looking like a forgotten () call. Regardless, it is now gone.

Bergi
  • 630,263
  • 148
  • 957
  • 1,375