Logical Operators in Typescript

TypeScript logical operators are used to combine logical conditions and evaluate their truthfulness. They play a crucial role in controlling program flow and making decisions based on multiple criteria.

List of Logical Operators

Here are the significant logical operators available in TypeScript:

  1. And (&&): Returns true only if both operands are true.
  2. Or (||): Returns true if at least one operand is true.
  3. Not (!): Inverts the truth value of the operand.

Logical AND (&&)

The logical AND operator (&&) returns true if both operands are true; otherwise, it returns false.

let x: boolean = true; let y: boolean = false; console.log(x && y); // Result: false

In this example, the result is false because both x and y are not true.

Logical OR (||)

The logical OR operator (||) returns true if at least one of the operands is true; otherwise, it returns false.

let a: boolean = true; let b: boolean = false; console.log(a || b); // Result: true

Here, the result is true because at least one of the operands (a) is true.

Logical NOT (!)

The logical NOT operator (!) negates a Boolean value. If the operand is true, ! makes it false; if the operand is false, ! makes it true.

let isSunny: boolean = true; console.log(!isSunny); // Result: false

In this example, !isSunny evaluates to false because isSunny is true.

Combining Logical Operators

Logical operators can be combined to create more complex conditions.

let hasPermission: boolean = true; let isLoggedIn: boolean = false; let canAccessResource: boolean = hasPermission && isLoggedIn; console.log(canAccessResource); // Result: false

Here, canAccessResource is false because both conditions (hasPermission and isLoggedIn) need to be true for the logical AND (&&) to evaluate to true.

Ternary Conditional Operator (? :)

The ternary conditional operator (? :) provides a concise way to express a conditional statement. It returns one of two values based on a condition.

let age: number = 25; let isAdult: boolean = age >= 18 ? true : false; console.log(isAdult); // Result: true

In this example, isAdult is true because the condition age >= 18 is true.

Short-Circuit Evaluation

The && and || operators utilize short-circuit evaluation. This means that they stop evaluating operands once the overall outcome is determined. This optimization can improve performance and code efficiency.

Truthiness and Falsiness

TypeScript recognizes certain values as inherently true or false. These values are called "truthy" and "falsy," respectively. Truthy values include true, non-zero numbers, strings (except empty strings), and non-null objects. Falsy values include false, zero, empty strings, and null. This concept is relevant when evaluating logical expressions.

Operator Precedence

Logical operators follow a specific order of precedence within expressions. Understanding this order ensures accurate evaluation and avoids ambiguity in your code.

Additional Features

  1. Nullish coalescing operator (??): Provides a concise way to check for null or undefined values and assign a fallback value.
  2. Ternary operator (? :): Allows for conditional statement construction based on the truth value of an expression.

Conclusion

TypeScript logical operators, including AND (&&), OR (||), and NOT (!), are essential for evaluating Boolean conditions and creating complex decision structures. These operators allow developers to combine and manipulate Boolean values, enabling the creation of flexible and dynamic logic in TypeScript programs. The ternary conditional operator (? :) further provides a concise way to express conditional statements based on Boolean conditions.