# Logical Operators

Recall that an operator is one or more characters that carries out an action on its operand(s). In Data and Variables we learned about three types of operators:

• Arithmetic operators, such as `+`, `-`, `*`, `/`, and `%`.
• The string operator `+`.
• Compound assignment operators, such as `+=` and `-=`.

Arithmetic and string operators take number and string operands, respectively, returning values of the same type. Compound assignment operators work similarly with numbers or strings while also reassigning the value of the first, variable operand.

## Boolean Operators

In addition to these operators, we learned about comparison operators like `===`, `<`, and others. These operators are part of a larger class known as boolean operators, so-called because they return a boolean value (`true` or `false`).

Three additional boolean operators allow us to create more complex expressions. These are described below.

### Logical AND

A compound boolean expression is a boolean expression built out of smaller boolean expressions. JavaScript allows us to create a compound boolean expression using the logical AND operator, `&&`.

The operator takes two operands, and the resulting expression is `true` if both operands are `true` individually. If either operand is `false`, the overall expression is `false`.

Lets see how this works in code.

In line 1, `7 > 5 && 5 > 3` evaluates to `true` because both `7 > 5` and `5 > 3` are `true` individually.

The expression `7 > 5 && 2 > 3` evaluates to `false` because one of the two expressions, `2 > 3`, is `false`.

Like line 2, line 3 returns `false` because both sub-expressions are `false`. Notice that we can mix and match data types however we like, as long as both sides of the `&&` expression are themselves boolean expressions.

### Logical OR

JavaScript’s logical OR operator, `||`, also creates compound boolean expressions. This operator takes two operands, and the resulting expression is `true` if either of the operands are `true` individually. If both operands are `false`, the overall expression is `false`.

Let’s look at some examples in JavaScript.

``````console.log(7 > 5 || 5 > 3);
console.log(7 > 5 || 2 > 3);
console.log(2 > 3 || 'dog' === 'cat');
``````

Console Output

``````true
true
false
``````

## Logical NOT

The logical NOT operator, `!`, takes only a single operand and reverses its boolean value.

The operator `!` (sometimes called a “bang”) has the same semantic role as the word “not” in English.

## Operator Precedence

We now have a number of operators in our toolkit. It is important to understand how these operators relate to each other with respect to operator precedence. Operator precedence is the set of rules that dictate in which order the operators are applied.

JavaScript will always apply the logical NOT operator, `!`, first. Next, it applies the arithmetic operators, followed by the comparison operators. The logical AND and OR are applied last.

This means that the expression `x * 5 >= 10 && y - 6 <= 20` will be evaluated so as to first perform the arithmetic and then check the relationships. The `&&` evaluation will be done last. The order of evaluation is the same as if we were to use parentheses to group, as follows:

``````((x * 5) >= 10) && ((y - 6) <= 20)
``````

While parentheses are not always necessary due to default operator precedence, they make expressions much more readable. As a best practice, we encourage you to use them, especially for more complicated expressions.

The following table lists operators in order of precedence, from highest (applied first) to lowest (applied last). A complete table for the entire language can be found in the MDN JavaScript Documentation .

PrecedenceCategoryOperators
(highest)Logical NOT`!`
Exponentiation`**`
Multiplication and division`*`, `/`, `%`
Addition and subtraction`+`, `-`
Comparison`<=`, `>=`, `>`, `<`
Equality`===`, `!==`, `==`, `!=`
Logical AND`&&`
(lowest)Logical OR`

## Truth Tables

Truth tables help us understand how logical operators work by calculating all of the possible return values of a boolean expression. Let’s look at the truth table for `&&`, which assumes we have two boolean expressions, A and B, joined by `&&`.

Consider the first row of the truth table. This row states that if A is true and B is true, then A && B is true. This is a fact, regardless of what boolean expressions A and B might actually be. The two middle rows demonstrate that if either A or B is false, then A && B is false. (If this idea is hard to grasp, try substituting actual expressions for A and B.)