TypeScript Logical Operators

Introduction

In this chapter, we will explore logical operators in TypeScript. Logical operators are used to perform logical operations and return a boolean result. They are typically used in conditional statements to control the flow of a program based on certain conditions.

Table of Contents

  1. Definition
  2. Types of Logical Operators
    • Logical AND (&&)
    • Logical OR (||)
    • Logical NOT (!)
  3. Examples and Output
  4. Conclusion

Definition

Logical operators in TypeScript are symbols used to combine or invert boolean values. They are essential for evaluating multiple conditions in a single expression and making decisions based on the result.

Types of Logical Operators

Logical AND (&&)

Definition

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

Syntax

let result = operand1 && operand2; 

Example

This example demonstrates the use of the logical AND operator.

let a: boolean = true; let b: boolean = false; let andResult = a && b; console.log(andResult); 

Output

false 

Logical OR (||)

Definition

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

Syntax

let result = operand1 || operand2; 

Example

This example demonstrates the use of the logical OR operator.

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

Output

true 

Logical NOT (!)

Definition

The logical NOT operator inverts the value of its operand. If the operand is true, it returns false; if the operand is false, it returns true.

Syntax

let result = !operand; 

Example

This example demonstrates the use of the logical NOT operator.

let a: boolean = true; let notResult = !a; console.log(notResult); 

Output

false 

Examples and Output

Example 1: Using Logical Operators

In this example, we will use all the logical operators to evaluate different conditions.

TypeScript Code (src/index.ts)

let a: boolean = true; let b: boolean = false; // Logical AND let andResult = a && b; console.log("Logical AND: " + andResult); // Output: false // Logical OR let orResult = a || b; console.log("Logical OR: " + orResult); // Output: true // Logical NOT let notResult = !a; console.log("Logical NOT: " + notResult); // Output: false 

Compiling to JavaScript

To compile the TypeScript code to JavaScript, run the TypeScript compiler:

tsc src/index.ts 

Output in JavaScript (src/index.js)

var a = true; var b = false; // Logical AND var andResult = a && b; console.log("Logical AND: " + andResult); // Output: false // Logical OR var orResult = a || b; console.log("Logical OR: " + orResult); // Output: true // Logical NOT var notResult = !a; console.log("Logical NOT: " + notResult); // Output: false 

Running the JavaScript

To see the output of the compiled JavaScript code, run the JavaScript file using Node.js:

node src/index.js 

Conclusion

In this chapter, we covered logical operators in TypeScript, including logical AND, logical OR, and logical NOT operators. We provided examples with their outputs to illustrate how these operators work in TypeScript.

Leave a Comment

Scroll to Top