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.
Comments
Post a Comment
Leave Comment