!! Typically used to cast a subsequent expression to a Boolean type of data (Boolean), which is only true or false;
Often see examples of this:
The code is as follows:
var A;
var b=!! A
A default is undefined.!a is true,!! A is false, so the value of B is false, not undefined, and not other values, primarily for subsequent judgment.
!! Typically used to cast a subsequent expression to a Boolean type of data (Boolean), which is only true or false;
Because JavaScript is a weakly typed language (a variable has no fixed data type), it is sometimes necessary to cast it to the appropriate type, like this:
A=parseint ("1234″")
A= "1234″-0//Convert to Number
b=1234+ ""///Convert to String
C=someobject.tostring ()//Convert an object to a string
Where the 1th and 4th types are explicit conversions, 2, 3 are implicit conversions
Boolean conversions, JavaScript Convention rules for
False, undefinded, NULL, 0, False
True, 1, "somestring", [Object] is True
For other implicitly-converted values, such as null and undefined, the! Operator yields true results, so the two exclamation points are used to convert these values to "equivalent" Boolean values;
Take another look:
var foo;
alert (!foo);//undifined case, an exclamation point returns true;
alert (!goo);//null, an exclamation point returns true;
var o={flag:true};
var test=!! o.flag;//equivalent to Var test=o.flag| | False
alert (test);
This example shows that when undifined and Null are returned with an exclamation point that is true and returns false with two exclamation marks, the effect of the two exclamation point is that if the value of the variable (not null/undifined/0/"equivalent) is explicitly set, The result is returned based on the actual value of the variable, and if there is no setting, the result returns false.