The boolean type is used to represent one of two values:
true or false. Boolean values are commonly
used for conditions, decisions, login status, validation, permissions,
and application states.
true or false.
Do not put quotes around Boolean values.
A Boolean is a data type that has only two possible values:
true and false.
let isStudent: boolean = true;
let isAdmin: boolean = false;
Both variables have the type boolean.
You can explicitly specify the Boolean type using
: boolean.
let isActive: boolean = true;
let isLoggedIn: boolean = false;
The type annotation tells TypeScript that these variables can contain Boolean values.
TypeScript can automatically determine that a variable contains a Boolean value.
let isStudent = true;
let isAdmin = false;
TypeScript automatically infers both variables as
boolean.
The value true represents a positive or enabled state.
let isAvailable: boolean = true;
console.log(isAvailable);
Output:
true
The value false represents a negative or disabled state.
let isAvailable: boolean = false;
console.log(isAvailable);
Output:
false
Boolean values should be written as true and
false, without quotation marks.
let isLoggedIn: boolean = true;
This is different from a string:
let status: string = "true";
The first value is a Boolean. The second value is a string.
You can use const when the Boolean variable should not be
reassigned.
const isOnline: boolean = true;
console.log(isOnline);
A Boolean declared using let can be changed from
true to false or from false to
true.
let isLoggedIn: boolean = false;
isLoggedIn = true;
console.log(isLoggedIn);
A variable declared as boolean cannot receive a string.
let isActive: boolean = true;
// Error
// isActive = "true";
TypeScript checks that the assigned value has the correct type.
A Boolean variable cannot be assigned a number.
let isStudent: boolean = true;
// Error
// isStudent = 1;
Use actual Boolean values instead of numbers.
Comparison expressions produce Boolean results.
let age: number = 20;
let isAdult: boolean = age >= 18;
console.log(isAdult);
Output:
true
The strict equality operator === checks whether two
values are equal and have compatible types.
let age: number = 20;
let result: boolean = age === 20;
console.log(result);
Output:
true
The !== operator checks whether two values are not
strictly equal.
let age: number = 20;
let result: boolean = age !== 25;
console.log(result);
Output:
true
The > operator returns a Boolean value.
let marks: number = 80;
let result: boolean = marks > 40;
console.log(result);
Output:
true
The < operator also produces a Boolean result.
let age: number = 15;
let result: boolean = age < 18;
console.log(result);
Output:
true
Boolean values are frequently used with if statements.
let isLoggedIn: boolean = true;
if (isLoggedIn) {
console.log("Welcome");
}
The code inside the if block runs because the condition is
true.
let isLoggedIn: boolean = false;
if (isLoggedIn) {
console.log("Welcome");
} else {
console.log("Please login");
}
The else block executes when the Boolean value is
false.
The logical AND operator && returns true
when both conditions are true.
let isLoggedIn: boolean = true;
let isAdmin: boolean = true;
let result: boolean =
isLoggedIn && isAdmin;
console.log(result);
Output:
true
The logical OR operator || returns true when
at least one condition is true.
let isAdmin: boolean = false;
let isTeacher: boolean = true;
let result: boolean =
isAdmin || isTeacher;
console.log(result);
Output:
true
The logical NOT operator ! reverses a Boolean value.
let isLoggedIn: boolean = true;
let result: boolean = !isLoggedIn;
console.log(result);
Output:
false
Functions can accept Boolean parameters and return Boolean values.
function isAdult(age: number): boolean {
return age >= 18;
}
console.log(
isAdult(20)
);
The return type of this function is boolean.
Boolean values are commonly stored as object properties.
let student: {
name: string;
isActive: boolean;
} = {
name: "Rahul",
isActive: true
};
console.log(student.isActive);
You can create an array containing Boolean values using
boolean[].
let results: boolean[] = [
true,
false,
true,
true
];
console.log(results);
Every element in the array is a Boolean value.
Boolean variables are useful for storing validation results.
let userName: string = "Rahul";
let isValid: boolean =
userName.length > 0;
console.log(isValid);
The variable isValid tells us whether the condition is
satisfied.
A Boolean is commonly used to represent whether a user is logged in.
let isLoggedIn: boolean = false;
isLoggedIn = true;
if (isLoggedIn) {
console.log("Dashboard");
}
React applications frequently use Boolean values to control whether something should be displayed.
const isLoggedIn: boolean = true;
function App() {
return (
<div>
{isLoggedIn && <p>Welcome</p>}
</div>
);
}
React Native applications commonly use Boolean values for login status, loading state, visibility, permissions, and other conditions.
const isLoggedIn: boolean = true;
if (isLoggedIn) {
console.log("Show Dashboard");
} else {
console.log("Show Login Screen");
}
A Boolean can represent whether an application is currently loading.
let isLoading: boolean = true;
if (isLoading) {
console.log("Loading...");
} else {
console.log("Data Loaded");
}
This type of state is common in React Native applications when working with APIs.
Mistake 1: Using quotes around Boolean values.
// This is a string
let result = "true";
// This is a Boolean
let correctResult = true;
Mistake 2: Assigning a number to a Boolean.
let isActive: boolean = true;
// Error
// isActive = 1;
Always use true or false for Boolean values.
| Feature | Example |
|---|---|
| Boolean annotation | let active: boolean = true; |
| True value | true |
| False value | false |
| AND | true && false |
| OR | true || false |
| NOT | !true |
| Comparison | age >= 18 |
boolean type has only two values:
true and false. Boolean values are especially
useful for conditions and application states.
boolean type represents true or false.: boolean.if statements.&& represents logical AND.|| represents logical OR.! represents logical NOT.Question: Which two values can a Boolean variable contain?