1

Possible Duplicate:
What is the !! (not not) operator in JavaScript?
Reference - What does this symbol mean in JavaScript?

I find the js code write like this : !!undefined , !!false;

the jquery source code (jQuery 1.7.0.js: Line 748):

grep: function( elems, callback, inv ) {       
    var ret = [], retVal;        
    inv = !!inv;         
    // Go through the array, only saving the items       
    // that pass the validator function        
    for ( var i = 0, length = elems.length; i < length; i++ ){              
        retVal = !!callback( elems[ i ], i );            
        if ( inv !== retVal ) {                
            ret.push( elems[ i ] );            
        }        
    }         
    return ret;    
}
Community
  • 1
  • 1
Thinking80s
  • 2,802
  • 3
  • 14
  • 16
  • Well, that does `!expr` do? What does `!(!expr)` do? Put the little parts together into a bigger part :) –  Mar 09 '12 at 05:28
  • 1
    possible duplicate of [Reference - What does this symbol mean in JavaScript?](http://stackoverflow.com/questions/9549780/reference-what-does-this-symbol-mean-in-javascript), more specifically [What is the !! (not not) operator in JavaScript?](http://stackoverflow.com/questions/784929/what-is-the-not-not-operator-in-javascript) – Wesley Murch Mar 09 '12 at 05:30

3 Answers3

1

! means opposite

So !! means double opposite.

It is also commonly used in this case:

var check = !!(window.something && window.runthis)
//If something exists and runthis exists, then
//check = true

//If one of them is not exist, then
//check = false

Commonly used in checking browser compatibly.

Derek 朕會功夫
  • 84,678
  • 41
  • 166
  • 228
0

It's a shorthand way of casting a non-boolean object to a boolean.

For example, !!0 would convert to false.

sethcall
  • 2,637
  • 1
  • 16
  • 21
0

its simply double negation used to coerce any data type (null, undefined, objects) to boolean

http://navirudra.com/blog/javascript/javascript-double-negative-trick.html

labroo
  • 2,629
  • 2
  • 27
  • 35