Today, I read the source code of qunit and found a strange piece of code. Although I can understand what it means, I don't understand the role of the double exclamation mark.
Copy codeThe Code is as follows:
Function id (name ){
Return !! (Typeof document! = "Undefined" & document. getElementById )&&
Document. getElementById (name );
}
Then I checked some information on the Internet, which is equivalent to a ternary operator and returns a boolean value.
Copy codeThe Code is as follows:
Var ret = !! Document. getElementById
It is equivalent:
Copy codeThe Code is as follows:
Var ret = document. getElementById? True: false;
If the value is a non-null string or a non-zero number, true is returned. If the value is a null String, 0, or null, false is returned.
Copy codeThe Code is as follows:
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