Determine the double exclamation mark in js. I checked some information on the Internet. He is equivalent to a ternary operator and returns a boolean value. Today, I read the qunit source code and found a strange piece of code. Although I can understand what he meant, but I do not understand the role of the Two exclamation points.
The 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.
The Code is as follows:
Var ret = !! Document. getElementById
It is equivalent:
The 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.
The 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