Why two !!s in an IF statement when using &&?

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') {
}

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 .


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.


  • & 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).
  • 链接地址: http://www.djcxy.com/p/12662.html

    上一篇: 区别! 和! 在JavaScript中

    下一篇: 为什么在使用&时在IF语句中有两个!!