7

Possible Duplicate:
What is the !! (not not) operator in JavaScript?

I'm looking through some code and see an IF statement that looks like the one below. Can anyone tell me why there are two !!s instead of one? I've never seen this before and can't dig anything up on Google because it's ignoring the special character.

if (!!myDiv && myDiv.className == 'visible') {
}
Community
  • 1
  • 1
Zoolander
  • 2,293
  • 5
  • 27
  • 37

6 Answers6

4

The double not operator is used to cast a variable to the boolean type. The dobule nots cancel each other out, but seeing as ! returns true or false, you only get one of the two output.

For example,

!!0 == true

So

!!myDiv == true

Casts myDiv to a boolean and tests it against true. !!myDiv will only give true or false.

Bojangles
  • 99,427
  • 50
  • 170
  • 208
  • Wow, that's cool. Dug up this link based on Jam's answer. http://www.bennadel.com/blog/1784-Using-Double-Not-Operator-For-Boolean-Type-Casting.htm – mrtsherman Feb 10 '12 at 15:46
  • 2
    Why is this even necessary in the example given in the question? The predicate of an `if` condition is already interpreted as a boolean. Also, `!!0 !== true`, since `0` is falsy. – Peter Olson Jan 08 '13 at 17:14
0

The double-bang (!!) converts the value to a true boolean. The first bang "not's" the potentially truthy/falsy value to a proper boolean and the second "not's" it back to what the value should be as a proper boolean.

pete
  • 24,141
  • 4
  • 37
  • 51
0

!! will coerce any object into a Boolean. It will evaluate to true for non-falsy values. But its not magic, its just a double not.

          !!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 
John Slegers
  • 45,213
  • 22
  • 199
  • 169
J. Holmes
  • 18,466
  • 5
  • 47
  • 52
0
  • & is a bitwise comparion (resulting in a value >= 0).
  • && is a logical comparision (resulting in true or false).
  • = is assignment (always evaluates as true).
  • == is a logical comparison (resulting in true or false).
twilson
  • 2,062
  • 14
  • 19
0

!!myDiv means twice the operator ! (equals to not).

myDiv seems to be a class, !myDiv results in a boolean (false or true) and again ! will reverse this boolean again (not resulting in the class instance (pointer)).

You also could write (myDiv != null).

Personally I prefer myDiv != null but !!myDiv is shorter.

Michel Keijzers
  • 15,025
  • 28
  • 93
  • 119
0

It's the double bang operator - it converts myDiv to a boolean value.

Stuart Golodetz
  • 20,238
  • 4
  • 51
  • 80