Learn Typescript Logical operators & Examples

Understanding TypeScript Logical Operators

Logical operators are fundamental operators in programming languages.

They compare Boolean expressions and return Boolean values. Boolean expressions consist of multiple conditions, where each condition returns true or false. These conditions can be combined using these logical operators, resulting in a Boolean output.

The syntax and usage are the same in JavaScript as well.

These operators are also called boolean operators in TypeScript.

The operand is an expression that evaluates to true/false. There are three logical operators in TypeScript: And, OR, and Not.

There are two kinds of operands:

  • Binary Operators: Work with two operands (Logical AND, OR)
  • Unary Operator: Works with one operand (Logical Not)

Here is the syntax for logical operators:

// Binary Operator syntax
Operand operator operand
// Unary Operator syntax
operator operand

Typescript Logical OR Operator

The symbol for this operator is two vertical lines (||). The operand is a conditional expression that evaluates to a boolean. Multiple operands can be combined, with precedence from left to right.

Below are examples demonstrating the possible conditional values for the OR operator.

console.log(true || true); // returns true
console.log(false || true); // returns true
console.log(true || false); // returns true
console.log("string1" || "string2"); // returns "string1"
console.log(false || "string3"); // returns "string3"
console.log("string4" || false); // returns "string4"
console.log("" || false); // returns false
console.log(false || ""); // returns ""

If one of the operands is true, the output returns true; otherwise, it returns false. All expressions return true except when both conditions are false.

Important notes:

  • The operator always evaluates from left to right.
  • If the first operand is true, the remaining operands are not evaluated, and the operator returns true.
  • If all operands are evaluated, subsequent operands from the left are false.

TypeScript Logical And Operator

The operator symbol is double ampersands (&&). This operator returns true if all operands are evaluated as true; otherwise, it returns false.

console.log(true && true); // returns true
console.log(false && true); // returns false
console.log(true && false); // returns false
console.log("string1" && "string2"); // returns "string2"
console.log(false && "string3"); // returns "false"
console.log("string4" && false); // returns "false"
console.log("" && false); // returns ""
console.log(false && ""); // returns false

Important notes:

  • It always evaluates from left to right.
  • If the first operand evaluates as false, it returns false, and other operations are not evaluated.
  • If all expressions evaluate, it returns the last operand evaluated value as output.

TypeScript Logical Not Operator

The symbol for the Not operator is an exclamation mark (!). This operator returns the inverse of the operand value.

console.log(!true); // returns false
console.log(!false); // returns true

Conclusion

In this tutorial, we learned about logical operators with examples. Expressions are evaluated from left to right for these operators.