Converting AND to OR in JavaScript
Tags: programming, javascript, computerscience, algorithms
Published: 2021-12-21 17:00:00

### 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yym18z269wupq9q7nbyo.png)

### 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/a17pmq6jeleruwpat0b7.png)

- **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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hbiiqapo8ikx8e77no7x.png)

- **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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1m1uhugunlvmrgaxluk7.png)

## 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
```
![XOR table](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mdr6h1tutqm8u4r84duy.png)

- 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hi1ydfqbl9vnnrdb4y8p.png)

### 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
```
to
```
!(!bCondition1 || !bCondition2)
```

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

For feedback on talk to me on [Twitter](https://twitter.com/biantris_)
If you want to continue supporting my content [Patreon] (https://www.patreon.com/beatrizoliveira)
My GitHub https://github.com/biantris

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

------