If logical not ! will switch a boolean value from false to true or true to false and a double not !! will cast a value from one primitive type to boolean is there any reason one can not use a logical not with a double not to create a triple not !!! to cast a value to a boolean and then switch it!?! Is this what creates dark matter?

var test = !!"1"; // cast to boolean
var test2 = !!""; // cast to boolean
var test3 = !true; // switch true to false
var test4 = !false; // switch to true
var test5 = !!!"1"; // cast to boolean and switch to false
var test6 = !!!""; // cast to boolean and switch to true


console.log("values");
console.log("test:", test); // true
console.log("test2:", test2); // false
console.log("test3:", test3); // false
console.log("test4:", test4); // true
console.log("test5:", test5); // false
console.log("test6:", test6); // true

console.log("types");
console.log("test:", typeof test); // boolean
console.log("test2:", typeof test2); // boolean
console.log("test3:", typeof test3); // boolean
console.log("test4:", typeof test4); // boolean
console.log("test5:", typeof test5); // boolean
console.log("test6:", typeof test6); // boolean
有帮助吗?

解决方案

You can triple a not operator, but that doesn't really buy you anything except code obfuscation.

The single not operator (!) converts its operand to a boolean and yields the inverse boolean value. By doubling the not operator (!!), you get the inverse of the inverse value, which is effectively the same as casting to a boolean.

Once you are operating on a boolean value, a double not will just cancel each other out and you can just as safely remove both operators.

You can easily test this for yourself

var test1 = !"1";   // cast to boolean and inverted
var test2 = !"";    // cast to boolean and inverted
var test3 = !!"1";  // cast to boolean
var test4 = !!"";   // cast to boolean
var test5 = !!!"1"; // cast to boolean and inverted
var test6 = !!!"";  // cast to boolean and inverted

console.log("values");
console.log("test1:", test1); // false
console.log("test2:", test2); // true
console.log("test3:", test3); // true
console.log("test4:", test4); // false
console.log("test5:", test5); // false
console.log("test6:", test6); // true

console.log("types");
console.log("test1:", typeof test1); // boolean
console.log("test2:", typeof test2); // boolean
console.log("test3:", typeof test3); // boolean
console.log("test4:", typeof test4); // boolean
console.log("test5:", typeof test5); // boolean
console.log("test6:", typeof test6); // boolean

其他提示

I'm not going to not tell you not to do this. I'm not sure this is not a not good idea or not. It might not be not confusing (not). It's not true that something is not not not true.

In short, do not not be not notty.

According to me, there is no difference between single not (!) and triple not (!!!). As triple not (!!!) is simply a double negation of single not (!). so, it is better to use single not (!). When we can perform the functionality with single not (!) then why should we make it complex with triple not (!!!).

E.x. (!!(!a)) (!a)

In the above example, there will be the same output of both expressions.

许可以下: CC-BY-SA归因
scroll top