Skip to main content

Logical Operators

Logical operators are used to check conditions with multiple expressions, e.g. in comparison operations.

Type casting

In logical operations, the types of operands are converted/cast to true or false. Casting occurs if a logical operator is found in the code.

Truthy and Falsy are the terms used for those values that are converted to true or false in a logical operation, although they were not originally booleans.

Note

Remember the 6 falsy values that are converted to false in logical conversion: 0, NaN, null, undefined, empty string and false. Everything else is converted to true.

Logical operators

There are three logical operators that are used to check the execution of multiple expressions.

logical operators

Logical AND

The && operator converts all operands to a boolean and returns the value of one of them. The left operand if it can be converted to false, or the right operand otherwise.

expression && expression

In the following example, both conditions will return true, so the result of the whole expression will be true, and the value of the right operand will be returned.

const age = 20;
console.log(age > 10 && age < 30); // true && true -> true

If at least one of the operands is converted to false, the expression will result in its value.

const age = 50;
console.log(age > 10 && age < 30); // true && false -> false
console.log(age > 80 && age < 120); // false && true -> false

That is, the logical AND stumbles on false and returns what it stumbles on or the last operand.

console.log(1 && 5); // true && true -> 5
console.log(5 && 1); // true && true -> 1
console.log(0 && 2); // false && true -> 0
console.log(2 && 0); // true && false -> 0
console.log("" && "Mango"); // false && true -> ""
console.log("Mango" && ""); // true && false -> ""
console.log("Mango" && "Poly"); // true && true -> "Poly"
console.log("Poly" && "Mango"); // true && true -> "Mango"
Note

When executing the logical AND, evaluating the right operand is not required if the left one is converted to false.

Logical OR

The || operator converts all operands to a boolean and returns the value of one of them: the left operand if it can be converted to true, the right operand otherwise.

expression || expression

In the following example, the condition will return true, so the result of the whole expression will be true, and the value of the first operand (converted to true) will be returned.

const age = 5;
console.log(age < 10 || age > 30); // true || false -> true

The result will also be true, since at least one of the operands, in this case the right one, is converted to true.

const age = 40;
console.log(age < 10 || age > 30); // false || true -> true

None of the conditions is met, so you get false, i.e. the value of the last operand.

const age = 20;
console.log(age < 10 || age > 30); // false || false -> false

That is, the logical OR stumbles on true and returns what it stumbles on or the last operand.

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

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

When executing the logical OR, evaluating the right operand is not required if the left one is converted to true.

Logical NOT

All the operators above are binary, containing two operands, left and right. The logical NOT is a unary operator, operating with just one operand on the right.

!expression

The ! operator converts the operand to a boolean, if necessary, and then inverses, i.e. replaces it with the opposite true -> false or false -> true.

console.log(!true); // false
console.log(!false); // true
console.log(!3); // !3 -> !true -> false
console.log(!"Mango"); // !"Mango" -> !true -> false
console.log(!0); // !0 -> !false -> true
console.log(!""); // !"" -> !false -> true

const isOnline = true;
const isNotOnline = !isOnline; // !isOnline -> !true -> false

Additional materials