🎓 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 the boolean type in TypeScript. The boolean type is used to represent logical values: true and false. Understanding how to work with booleans is essential for making decisions and controlling the flow of your TypeScript programs.
Table of Contents
- Definition
- Boolean Syntax
- Basic Operations with Booleans
- Common Boolean Methods
- Complete Example with Output
- Conclusion
Definition
In TypeScript, the boolean type is used to represent one of two values: true or false. Booleans are commonly used in conditional statements and loops to control the flow of a program.
Boolean Syntax
Syntax
let variableName: boolean = true;
Example
let isStudent: boolean = true;
let hasGraduated: boolean = false;
console.log(isStudent, hasGraduated);
Output
true false
Basic Operations with Booleans
TypeScript supports various basic operations on booleans, such as logical AND, logical OR, and logical NOT.
Example
let isStudent: boolean = true;
let hasGraduated: boolean = false;
// Logical AND
let andResult: boolean = isStudent && hasGraduated;
console.log(`Logical AND: ${andResult}`); // Output: false
// Logical OR
let orResult: boolean = isStudent || hasGraduated;
console.log(`Logical OR: ${orResult}`); // Output: true
// Logical NOT
let notResult: boolean = !isStudent;
console.log(`Logical NOT: ${notResult}`); // Output: false
Output
Logical AND: false
Logical OR: true
Logical NOT: false
Common Boolean Methods
TypeScript provides the Boolean object for working with boolean values, although it is rarely needed since most boolean operations are done using operators.
Example
let isStudent: boolean = true;
let hasGraduated: boolean = Boolean(0); // Converts 0 to false
console.log(isStudent); // Output: true
console.log(hasGraduated); // Output: false
Output
true
false
Complete Example with Output
In this section, we will combine all the examples into a single TypeScript file, compile it to JavaScript, and run it to see the output.
TypeScript Code (src/index.ts)
// Basic Boolean Operations
let isStudent: boolean = true;
let hasGraduated: boolean = false;
// Logical AND
let andResult: boolean = isStudent && hasGraduated;
console.log(`Logical AND: ${andResult}`); // Output: false
// Logical OR
let orResult: boolean = isStudent || hasGraduated;
console.log(`Logical OR: ${orResult}`); // Output: true
// Logical NOT
let notResult: boolean = !isStudent;
console.log(`Logical NOT: ${notResult}`); // Output: false
// Using Boolean Object
hasGraduated = Boolean(0); // Converts 0 to false
console.log(isStudent); // Output: true
console.log(hasGraduated); // 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)
// Basic Boolean Operations
var isStudent = true;
var hasGraduated = false;
// Logical AND
var andResult = isStudent && hasGraduated;
console.log("Logical AND: " + andResult); // Output: false
// Logical OR
var orResult = isStudent || hasGraduated;
console.log("Logical OR: " + orResult); // Output: true
// Logical NOT
var notResult = !isStudent;
console.log("Logical NOT: " + notResult); // Output: false
// Using Boolean Object
hasGraduated = Boolean(0); // Converts 0 to false
console.log(isStudent); // Output: true
console.log(hasGraduated); // 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 the boolean type in TypeScript, including how to declare and use boolean variables, perform basic boolean operations, and work with the Boolean object. We provided a complete example with its output to illustrate how booleans 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