Do you want to know about the Typescript boolean keyword? In this Typescript tutorial, I will explain to you how to use the boolean Keyword in Typescript with examples.
The TypeScript boolean keyword is used to declare variables that can hold two values: true or false. This is crucial for decision-making in code, like user authentication. For example, let isLoggedIn: boolean = false; declares a boolean variable to track a user’s login status, enhancing type safety and reliability in your code.
What is the TypeScript Boolean Keyword?
In TypeScript, boolean is a primitive type that represents a logical entity and can have two values: true or false. This is similar to the Boolean type in JavaScript, but TypeScript’s type checking adds an extra layer of security to ensure that variables are explicitly declared and used as booleans.
Typescript boolean keyword syntax
Here’s a simple example of how to declare a boolean variable in TypeScript:
let isActive: boolean = true;
In this case, isActive is a boolean variable that is explicitly set to true.
How to use boolean keyword in Typescript?
Let us check out a few real applications on how to use the boolean keyword in Typescript.
Example 1: User Authentication
In a user authentication scenario in Typescript, you might want to use a boolean to track whether a user is logged in or not. Here is the complete code:
class UserAuthentication {
isLoggedIn: boolean;
constructor() {
this.isLoggedIn = false;
}
login(username: string, password: string): boolean {
// Logic to authenticate
if (username === 'admin' && password === 'admin') {
this.isLoggedIn = true;
return true;
}
return false;
}
logout(): void {
this.isLoggedIn = false;
}
}
const auth = new UserAuthentication();
console.log(auth.login('admin', 'admin'));
auth.logout();
Once you run the above Typescript code, you can see the output below:
Example 2: Feature Toggle
Boolean variables are often used in feature toggles, which enable or disable features in an application. Here is a sample Typescript code:
class FeatureToggle {
isNewFeatureEnabled: boolean;
constructor() {
this.isNewFeatureEnabled = false;
}
enableNewFeature(): void {
this.isNewFeatureEnabled = true;
}
disableNewFeature(): void {
this.isNewFeatureEnabled = false;
}
checkFeatureStatus(): void {
if (this.isNewFeatureEnabled) {
console.log("New Feature is enabled.");
} else {
console.log("New Feature is disabled.");
}
}
}
const featureToggle = new FeatureToggle();
featureToggle.enableNewFeature();
featureToggle.checkFeatureStatus();
Example 3: Form Validation
Booleans are also useful in form validation scenarios in Typescript, where you might need to check if a form field is valid or not. Here is an example:
function isEmailValid(email: string): boolean {
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
return emailRegex.test(email);
}
console.log(isEmailValid('test@example.com')); // Returns true
console.log(isEmailValid('test@example')); // Returns false
Here you can see the output in the screenshot below, after I run the code using VS code.
Conclusion
The boolean keyword in TypeScript can be used in various real-world applications. From user authentication systems to feature toggles and form validation, booleans play a crucial role in controlling the flow of logic in your applications. In this tutorial, we discussed how to use the boolean Keyword in Typescript with a few real examples.
You may also like:
I am Bijay a Microsoft MVP (10 times – My MVP Profile) in SharePoint and have more than 17 years of expertise in SharePoint Online Office 365, SharePoint subscription edition, and SharePoint 2019/2016/2013. Currently working in my own venture TSInfo Technologies a SharePoint development, consulting, and training company. I also run the popular SharePoint website EnjoySharePoint.com