In TypeScript, the boolean type represents one of two values: true or false. These values are commonly used to control program flow, perform comparisons, and express conditions.
Declaring boolean variables in TypeScript is done by specifying the boolean type and assigning a value of true or false.
let isActive: boolean = true;
let isAuthenticated: boolean = false;
Copied!
Boolean Operators
Comparison Operators
Comparison operators return a boolean value as the result of comparing two values.
==(equality) and===(strict equality)!=(inequality) and!==(strict inequality)<,<=,>,>=
let x: number = 10;
let y: number = 20;
console.log(x == y); // false
console.log(x != y); // true
console.log(x < y); // true
console.log(x >= y); // false
Copied!
Logical Operators
TypeScript supports common logical operators for working with boolean values.
&& (Logical AND)
Returns true if both operands are true.
let a: boolean = true;
let b: boolean = false;
console.log(a && b); // false
Copied!
! (Logical NOT)
Inverts the boolean value.
console.log(!a); // false
console.log(!b); // true
Copied!
|| (Logical OR)
Returns true if at least one of the operands is true.
console.log(a || b); // true
Copied!
