Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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') {
}
like image 843
Zoolander Avatar asked Feb 10 '12 15:02

Zoolander


1 Answers

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.

like image 145
Bojangles Avatar answered Sep 28 '22 13:09

Bojangles