Search code examples
javascriptboolean-operations

Inline NOT operator using boolean


I have this snippet of code:

if (bool) {
    return <code that outputs bool>;
} else {
    return !<same code that outputs bool>;
}

Is there a way to convert the above to something like this so that I don't have to write the code twice?

return (bool? !! : !) <code>;

Solution

  • Found it!

    return bool === <code>
    

    I wrote down a truth table and saw this

          code
           T F
          +-+-
         T|T|F
    bool  +-+-
         F|F|T
    

    Which is the same table as bool = code