🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
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
- Definition
- Types of Logical Operators
- Logical AND (
&&) - Logical OR (
||) - Logical NOT (
!)
- Logical AND (
- Examples and Output
- 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.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment