Converting AND to OR in JavaScript
Beatriz Oliveira

Beatriz Oliveira @beatrizoliveira

About: Is creating Open Source/Learning Projects in Public and Articles.

Location:
Brasil
Joined:
Jan 9, 2021

Converting AND to OR in JavaScript

Publish Date: Dec 21 '21
17 3

Truth Table

  • First, it is good to review the concepts of the truth table, to understand how to input and boolean values work.

  • A truth table is nothing more than a logic machine that for a given input or value and its output is calculated according to logical, functional, and Boolean predeterminations

truth table

AND and OR

  • Recently I was faced with a lack of knowledge regarding Booleans, I didn't know how to convert a &&(AND) to ||(OR) in the language I was using (JavaScript), I would have to do a validation converting AND to OR so I focused on these two logical operators in this post.

  • Logical operators are nothing less than mathematical functions and expressions, yes the ones you probably studied in school.

truth table

  • The logical operator AND also called logical conjunction, is used for given two value inputs where it produces a true value if both operands are true. In programming logic, there are two values in bits that represent true or false which are: 1 and 0

AND table

  • Logical OR operator also called Logical Disjunction, given two inputs with values, it produces a true value if at least one of its operands is true.

OR table

Logical Operator XOR

  • The logical operator XOR represents the inequality function that is if both inputs are true or false the output will be false (0) if both inputs differ it will return true. A nice phrase to remember the logic of XOR is :
    "must have one or the other, but not both "

  • The analytical presentation of this logical operator is given by the expression:



f(a,b)=a+b-2ab


Enter fullscreen mode Exit fullscreen mode

XOR table

  • An interesting curiosity that you may not have realized yet is that the logical operator XOR is the conversion of the AND and OR operators, and so when you have a logical AND and want to turn it into a logical OR you will use the XOR.

table XOR Gate

Logical operator XOR in JavaScript

  • After a summary of logical operators and truth table we can now get back to solving the problem that triggered this post.
  • As in the truth table there are also logical operators in programming languages, and how would be the XOR operator in JavaScript?

Simple, since we do not have a real symbol for this operator we can represent it with the conversion of the AND to OR operators that would look like this:



bCondition1 && bCondition2


Enter fullscreen mode Exit fullscreen mode

to



!(!bCondition1 || !bCondition2)


Enter fullscreen mode Exit fullscreen mode

I hope this post has helped you or added something to it \o/

For feedback on talk to me on Twitter
If you want to continue supporting my content Patreon
My GitHub https://github.com/biantris

Version in Pt-BR 🇧🇷: https://dev.to/beatrizoliveira/convertendo-and-para-or-em-javascript-4ohd


Comments 3 total

  • Marcell Cruz
    Marcell CruzDec 28, 2021

    Great post!!!

  • Riccardo Bernardini
    Riccardo BernardiniMay 22, 2022

    This triggered a curiosity: why would you want to convert AND to OR?

    • Lã Quốc Nghị
      Lã Quốc NghịDec 23, 2022

      Example: When you use Regex to filter somethings, Regex doesn't have AND operator

Add comment