Back

TypeScript Boolean Type

The boolean type represents two possible values: true or false. It controls logic and conditions in applications.

Declaring Boolean Variables

let isLoggedIn: boolean = true;
let hasPermission: boolean = false;

Only true or false can be assigned to boolean variables. Other types cause errors.

Type Inference

let isActive = true;

TypeScript infers 'isActive' as boolean, simplifying code and ensuring type safety.

Using Boolean in Conditional Statements

if (isLoggedIn) {
  console.log("Welcome back!");
}

Boolean variables can be used directly in if/else statements, managing user state effectively.

Boolean Parameters in Functions

function toggleMenu(isOpen: boolean) {
  console.log(isOpen ? "Menu opened" : "Menu closed");
}

Functions with boolean parameters help manage UI states and logic with compile-time correctness.

Why Is Boolean Important?

Correct logic flow is critical for security and stability. TypeScript’s boolean type reduces errors and improves clarity.

Back