How Logical Operators like And, Or, and Not Work in JavaScript (With Examples!)

In JavaScript, you only have to know about 3 logical operators, and the key terms used here are pretty English.

You just have to remember that there’s an and, or, and a not which are all related to Boolean values.

Also, the “and” and “or” operator is binary operators, so you need to have 2 values when using them.

The not operator is actually a unary operator.

The and operator

The and operator is represented as &&. It’s also a binary operator which makes sense because it evaluates 2 values.

It’s also based on boolean and so the results are either going to be true or false.

So when you use the and operator, the 2 values are evaluated as either true or false, and the overall result of && is true, if and only if, both values on both sides are considered true.

If either or both values out of the two are false, then && will result in false.

Here’s an example:

Example 1: Using the and operator for 2 true values

console.log(true && true);

The answer here is:

true

Again, with the and operator, as long as both sides are true, then the overall expression is true.

Example 2: Using the and operator for a true and a false value

Let’s try true and false as the 2 values.

console.log(true && false);
// -> false

Here, you can see how the rules applied for && to be “all or nothing.”

Example 3: Using the and operator for 2 false values

console.log(false && false);
// -> false

Finally, if you have 2 false results, the overall is still false.

The or operator

Now the or operator is different. It’s not quite the opposite of the and operator but it takes the concept and reverses it slightly.

The or operator is denoted as the symbol || and it basically returns true if and only if either one of the two values is true.

That means that if you have one that’s true and the other false, it will return a true. If you have both of the values being true, then it will also return true as well. The only time it will return false is if both values are false.

Example 1: These are or operators that return true

console.log(true || false);
//true

The code above makes sense because it’s the or operator.

It’s basically saying, “I will output true if either the left value of me is true or the right value of me is true” and since the left value is true, then the entire result is true.

What about if both left and right side values are true?

console.log(true || true);
//true

It turns out that even if both sides of the or operator are true, then it will return a true statement.

Example 2: or operators that return false

The only time the or operator would return false is when both the left and right sides of the operator are false as well.

console.log(false || false);
//false

It seems like the only way to get a false result from the or operator is to have both left and right values false at the same time.

The not operator

The not operator is denoted as ! right next to and before a value. Again, it’s a unary operator.

It takes whatever it’s being put on, and transforms it into an opposite: False becomes true, and true becomes false.

Example 1: How to use the not operator on true and false

Here’s a look at where the ! goes, how to use it, and what it does.

console.log(!true);
//false

So if its “not” true, then it’s false.

Or:

console.log(!false);
//true

If it’s “not” false, then it’s true!

I wonder what would happen if you used numbers.

Example 2: Using the not operator on numbers

console.log(!1);
//false
console.log(!0);
//true
console.log(!234);
//false
console.log(!-23);
//false

If I can remember back, any integer, decimal, or number, whether it be a positive number or a negative number is considered true. The only type of number that’s false is 0.

So the results here make a lot of sense.

As you can see even the even the negative number is evaluated to be true when you place the not operator next to it.

Leave a Comment