Why two !! s in the expression IF using &&?

Possible duplicate:
What!! (not) in JavaScript?

I look through the code and see an IF statement that looks like the one below. Can anyone tell me why there are two !! instead of one? I have never seen this before and cannot throw something on Google because it ignores the special character.

if (!!myDiv && myDiv.className == 'visible') { } 
+7
source share
6 answers

The double operator is not used to translate a variable into a boolean type. Failures dobule cancel each other out, but seeing that ! returns true or false , you get only one of two output data.

For example,

 !!0 == true 

So

 !!myDiv == true 

Passes myDiv to boolean and checks it for true. !!myDiv will only give true or false .

+4
source

A double hit ( !! ) converts the value to a true Boolean. The first “no” hit is a potentially true / false value for the correct boolean, and the second “no” is that the value should be like the correct boolean.

0
source

!! will force any object to a logical one. It will evaluate true for non-fake values. But this is not magic, but simply double.

  !!false === false !!true === true !!0 === false !!parseInt("foo") === false // NaN is falsy !!1 === true !!-1 === true // -1 is truthy !!"" === false // empty string is falsy !!"foo" === true // non-empty string is truthy !!"false" === true // ...even if it contains a falsy value !!window.foo === false // undefined is falsy !!null === false // null is falsy !!{} === true // an (empty) object is truthy !![] === true // an (empty) array is truthy; PHP 
0
source
  • & - bitwise comparison (as a result, the value> = 0 is obtained).
  • && is a logical comparison (the result is true or false).
  • = - assignment (always evaluated as true).
  • == is a logical comparison (leading to true or false).
0
source

!! myDiv means double statement! (equal to zero).

myDiv seems to be class !! myDiv results in a boolean (false or true) and again! change this boolean value again (will not lead to an instance of the class (pointer)).

You can also write (myDiv! = Null).

Personally, I prefer myDiv! = Null, but !! myDiv is shorter.

0
source

This is a double bang statement - it converts myDiv to a boolean.

0
source

All Articles