Let's use a simple example to illustrate:
Copy Code code as follows:
var o={flag:true};
var test=!! o.flag;//equivalent to Var test=o.flag| | False
alert (test);
Because null and undefined use the! operator to produce true results, the effect of using two exclamation points is that if you explicitly set the value of flag in O (not null/undefined/0 ""/equivalent), The natural test takes the same value as O.flag, and if not set, the test defaults to false instead of null or undefined.
Some of the more classic examples in jquery are as follows: (jquery 1.7.0.js:line 748)
Copy Code code as follows:
Grep:function (Elems, callback, inv) {
var ret = [], retVal;
INV =!! Inv
Go through the array, only saving the items
This 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;
}
When using the grep function, if the third argument is given and is not null/undefined/0 ""/equivalent, the INV is true or false. The purpose of this is to ensure that the values of inv and retval can only be taken in true/false, not other values, to facilitate subsequent judgment.