Lesson 14 of 60 – Boolean Type
23%

Boolean Type in TypeScript

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.

Note: A Boolean value can only be true or false. Do not put quotes around Boolean values.

1. What is a Boolean?

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.

2. Boolean Type Annotation

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.

3. Boolean Type Inference

TypeScript can automatically determine that a variable contains a Boolean value.

let isStudent = true;

let isAdmin = false;

TypeScript automatically infers both variables as boolean.

4. true Value

The value true represents a positive or enabled state.

let isAvailable: boolean = true;

console.log(isAvailable);

Output:

true

5. false Value

The value false represents a negative or disabled state.

let isAvailable: boolean = false;

console.log(isAvailable);

Output:

false

6. Boolean Values Do Not Use Quotes

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.

7. Boolean with const

You can use const when the Boolean variable should not be reassigned.

const isOnline: boolean = true;

console.log(isOnline);

8. Changing a Boolean Variable

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);

9. Boolean Cannot Receive a String

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.

10. Boolean Cannot Receive a Number

A Boolean variable cannot be assigned a number.

let isStudent: boolean = true;

// Error
// isStudent = 1;

Use actual Boolean values instead of numbers.

11. Boolean with Comparison Operators

Comparison expressions produce Boolean results.

let age: number = 20;

let isAdult: boolean = age >= 18;

console.log(isAdult);

Output:

true

12. Equality Comparison

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

13. Not Equal Comparison

The !== operator checks whether two values are not strictly equal.

let age: number = 20;

let result: boolean = age !== 25;

console.log(result);

Output:

true

14. Greater Than Comparison

The > operator returns a Boolean value.

let marks: number = 80;

let result: boolean = marks > 40;

console.log(result);

Output:

true

15. Less Than Comparison

The < operator also produces a Boolean result.

let age: number = 15;

let result: boolean = age < 18;

console.log(result);

Output:

true

16. Boolean with if Statement

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.

17. Boolean with if...else

let isLoggedIn: boolean = false;

if (isLoggedIn) {

    console.log("Welcome");

} else {

    console.log("Please login");

}

The else block executes when the Boolean value is false.

18. Logical AND Operator

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

19. Logical OR Operator

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

20. Logical NOT Operator

The logical NOT operator ! reverses a Boolean value.

let isLoggedIn: boolean = true;

let result: boolean = !isLoggedIn;

console.log(result);

Output:

false

21. Boolean in Functions

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.

22. Boolean in Objects

Boolean values are commonly stored as object properties.

let student: {
    name: string;
    isActive: boolean;
} = {

    name: "Rahul",
    isActive: true

};

console.log(student.isActive);

23. Boolean in Arrays

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.

24. Boolean with Validation

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.

25. Boolean for Login Status

A Boolean is commonly used to represent whether a user is logged in.

let isLoggedIn: boolean = false;

isLoggedIn = true;

if (isLoggedIn) {

    console.log("Dashboard");

}

26. Boolean in React

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>
    );

}

27. Boolean in React Native

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");

}

28. Boolean for Loading State

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.

29. Common Boolean Mistakes

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.

30. Boolean Type Summary

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
Remember: The boolean type has only two values: true and false. Boolean values are especially useful for conditions and application states.

📌 Key Points

  • The boolean type represents true or false.
  • Boolean values are written without quotation marks.
  • You can explicitly use : boolean.
  • TypeScript can infer Boolean types automatically.
  • Boolean values are commonly used with if statements.
  • Comparison expressions produce Boolean results.
  • && represents logical AND.
  • || represents logical OR.
  • ! represents logical NOT.
  • Boolean values are widely used in React and React Native applications.

🧠 Quick Quiz

Question: Which two values can a Boolean variable contain?