Welcome back! I am Mihir, and in this lesson we will learn the boolean type in TypeScript.
A boolean can only be one of two values:
true
falseBooleans are used for conditions, flags, permissions, loading states, feature toggles, and form validation.
What is a boolean?
Use boolean for true or false values.
let isLoggedIn: boolean = true;
let hasAccess: boolean = false;
let isPublished: boolean = true;Invalid:
let isActive: boolean = "yes";"yes" is a string, not a boolean.
Type Inference with Booleans
You do not always need : boolean.
let isLoading = false;
let isAdmin = true;TypeScript infers both as boolean.
Invalid:
isLoading = "no";
isAdmin = 1;TypeScript already knows these variables should be booleans.
Booleans in Conditions
Booleans are commonly used with if statements.
let isLoggedIn: boolean = true;
if (isLoggedIn) {
console.log("Show dashboard");
} else {
console.log("Show login page");
}This makes your program choose between different paths.
Boolean Function Parameters
You can pass booleans into functions.
function setTheme(isDarkMode: boolean): void {
if (isDarkMode) {
console.log("Use dark theme");
} else {
console.log("Use light theme");
}
}
setTheme(true);Invalid:
setTheme("dark");The function expects a boolean.
Boolean Function Return Values
Functions can return booleans.
function isAdult(age: number): boolean {
return age >= 18;
}
const canVote = isAdult(21);canVote is a boolean.
Another example:
function hasMinLength(value: string, minLength: number): boolean {
return value.length >= minLength;
}This is common in form validation.
Booleans in Object Types
Booleans are often used as flags in objects.
type User = {
id: number;
name: string;
isAdmin: boolean;
emailVerified: boolean;
};
const user: User = {
id: 1,
name: "Mihir",
isAdmin: false,
emailVerified: true,
};The names should be clear and readable.
Good boolean names often start with:
ishascanshould
Boolean Expressions
Comparison expressions return booleans.
const age = 25;
const isAdult = age >= 18;TypeScript infers isAdult as boolean.
Other examples:
const isExact = "TypeScript" === "TypeScript";
const hasItems = [1, 2, 3].length > 0;
const isValidPrice = 999 > 0;Each result is a boolean.
Boolean Literal Types
You can use literal boolean types, but they are less common.
let alwaysTrue: true = true;Invalid:
alwaysTrue = false;You will usually use boolean, not true or false alone.
Boolean literal types become more useful in advanced patterns with discriminated unions.
Common Mistake: boolean vs Boolean
Use lowercase boolean:
let active: boolean = true;Avoid uppercase Boolean:
let active: Boolean = true;For normal true or false values, lowercase boolean is the correct choice.
Common Mistake: String Booleans
These are strings:
let value = "true";
let enabled = "false";They are not booleans.
If data comes from a URL, form, or API, convert it intentionally:
function parseBoolean(value: string): boolean {
return value === "true";
}Do not assume "false" behaves like false.
In JavaScript, "false" is a truthy string.
Quick Reference Summary
| Concept | Example |
|---|---|
| Boolean annotation | let active: boolean = true |
| Boolean inference | let active = true |
| Condition | if (active) {} |
| Function parameter | function toggle(value: boolean) |
| Function return | function isAdult(): boolean |
| Object flag | isAdmin: boolean |
| Boolean expression | age >= 18 |
| Preferred primitive | boolean, not Boolean |
Practice
Create a small validation function:
function canPublish(title: string, isDraft: boolean): boolean {
return title.trim().length > 0 && !isDraft;
}
const result = canPublish("TypeScript Basics", false);
console.log(result);Try passing "false" instead of false and see what TypeScript says.
What You've Learned
You now understand:
- What the
booleantype is - How boolean inference works
- How booleans are used in conditions
- How to type boolean parameters and returns
- How booleans work inside object types
- Why clear boolean names matter
- Why string booleans are not real booleans
- Why lowercase
booleanis preferred
What's Next?
In the next lesson, we will learn null and undefined in TypeScript.
These two values are important for missing data, loading states, optional fields, and strict null checking.
Need Help?
- Have questions, confusion, or want to know more? Contact me
Booleans are simple, but naming them well makes your code much easier to read.