2

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
3
  • 3
    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. Commented Aug 22, 2019 at 21:59
  • Your premise is wrong. There is no !! operator that does a logical not as opposed to a bitwise not. It would be interesting to learn why not... I am going to raise a question about this now! Commented Sep 30, 2019 at 5:28
  • 1
    It turns out I did not understand this very well and you do not claim the existence of a !! operator. My ignorance spawned a mighty interesting thread though: softwareengineering.stackexchange.com/questions/399096/… Commented Oct 1, 2019 at 19:05

3 Answers 3

15

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
2
  • So a single logical not is effectively the same as a triple not? Commented Aug 22, 2019 at 19:10
  • 3
    That's pretty much what he said, yes. Commented Aug 22, 2019 at 19:21
11

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.

3
  • 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
    Commented Aug 22, 2019 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
    Commented Aug 22, 2019 at 19:40
  • 2
    It makes for a pretty good self-demonstrating snippet of how excessive nots can be confusing.
    – 8bittree
    Commented Aug 22, 2019 at 19:56
0

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.

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