0

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
  • 1
    How many nots could a not not not if a not could not not a not? As many nots as a not could not not if a not could not not not. – Greg Burghardt Aug 22 at 21:59
10

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
  • So a single logical not is effectively the same as a triple not? – 1.21 gigawatts Aug 22 at 19:10
  • 1
    That's pretty much what he said, yes. – Robert Harvey Aug 22 at 19:21
5

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.

  • 2
    Translation: I'm going to tell you not to do this. I don't know if this is a good idea. It might be confusing (just kidding, it's understandable). It's false that something is false. In short, don't be notty. – 8bittree Aug 22 at 19:36
  • @8bittree I think I messed up the third sentence. I guess that just goes to not show this is not an unclear approach. – JimmyJames Aug 22 at 19:40
  • 2
    It makes for a pretty good self-demonstrating snippet of how excessive nots can be confusing. – 8bittree Aug 22 at 19:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.