DEV Community

Cover image for Converting AND to OR in JavaScript
Beatriz Oliveira
Beatriz Oliveira

Posted on • Updated on

Converting AND to OR in JavaScript

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:

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


!(!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

Version in Pt-BR 🇧🇷:

Top comments (3)

pinotattari profile image
Riccardo Bernardini

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

fakekey profile image
Lã Quốc Nghị

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

____marcell profile image
Marcell Cruz

Great post!!!