6

Possible Duplicate:
What is the !! operator in JavaScript?

What is the difference between these two operators? Does !! have special meaning, or does it simply mean you are doing two '!' operations. I know there are "Truth" and "Truthy" concepts in Javascript, but I'm not sure if !! is meant for "Truth"

Community
  • 1
  • 1
void.pointer
  • 24,859
  • 31
  • 132
  • 243

2 Answers2

8

!! is just double !

!true // -> false
!!true // -> true

!! is a common way to cast something to boolean value

!!{}  // -> true
!!null // -> false
bjornd
  • 22,397
  • 4
  • 57
  • 73
7

Writing !! is a common way of converting a "truthy" or "falsey" variable into a genuine boolean value.

For example:

var foo = null;

if (!!foo === true) {
    // Code if foo was "truthy"
}

After the first ! is applied to foo, the value returned is true. Notting that value again makes it false, meaning the code inside the if block is not entered.

Andrew Whitaker
  • 124,656
  • 32
  • 289
  • 307