Looking at the qunit source code today, I found a very strange piece of code. Although I can understand its meaning, I don't understand the role of the double exclamation mark.
function id( name ) {
return !! ( typeof document !== "undefined" && document && document.getElementById ) &&
document.getElementById( name );
}
Then I checked some information online, he was quite For the ternary operator, returns a boolean value.
var ret = !!document.getElementById
is equivalent to:
var ret = document.getElementById ? true : false;
Returns true when the value is a non-empty string and a non-zero number, returns false when the value is an empty string, 0 or null.
var a = " "; alert(!!a) ; //true
var a = "s"; alert(!!a); //true
var a = true; alert(!!a); //true
var a = 1; alert(!!a); //true
var a = -1; alert(!!a); //true
var a = -2; alert(!!a); //true
var a = 0; alert(!!a); //false
var a = ""; alert(!!a); //false
var a = false; alert(!! a); //false
var a = null; alert(!!a); //false