Skip to content

Instantly share code, notes, and snippets.

@d-simon
Created March 9, 2014 17:17
Show Gist options
  • Save d-simon/9451028 to your computer and use it in GitHub Desktop.
Save d-simon/9451028 to your computer and use it in GitHub Desktop.
Double NOT Operator ! (convert value to boolean)

Taken from Double exclamation points?

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

#Double NOT Operator

This converts a value to a boolean and ensures a boolean type.

"foo"      =    "foo"
!"foo"     =    false
!!"foo"    =    true

If foo.bar is passed through, then it may not be 0 but some other falsy value. See the following truth table:

Truth Table for javascript

''        ==   '0'           // false
0         ==   ''            // true
0         ==   '0'           // true
false     ==   'false'       // false
false     ==   '0'           // true
false     ==   undefined     // false
false     ==   null          // false
null      ==   undefined     // true
" \t\r\n" ==   0             // true

Source: Doug Crockford

Javascript also gets really weird when it comes to NaN values. And this is the only case I can think of off the top of my head where !! would behave differently to ===.

NaN   ===  NaN     //false
!!NaN === !!NaN    //true

// !!NaN is false
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment