TypeScript Booleans

Note: If you're new to TypeScript, check our Getting Started with TypeScript tutorial first.


In TypeScript, booleans are primitive data types that can either be true or false. For example,

let a = true;
let b = false;

Note: If you wrap true or false in a quote, then they are considered as a string. For example,

let a = 'true';
console.log(typeof a); // string

Booleans Results of Operators

The comparison and logical operators in TypeScript always return boolean values.

1. TypeScript Boolean With Comparison Operators

let age: number = 18;

// Check if age is greater than or equal to 18
let isAdult: boolean = age >= 18;

console.log(isAdult); // true

Here, age >= 18 evaluates if age is greater than or equal to 18 and assigns the result (true or false) to isAdult.

2. TypeScript Boolean With Logical Operators

let age: number = 18;
// Assign boolean value 'true' to hasConsent
let hasConsent: boolean = true;

let canDrive = age >= 16 && hasConsent; 
console.log(canDrive);  // true

In the above program, age >= 16 && hasConsent checks if age is greater than or equal to 16 and if hasConsent is true.

Here, the value of age >= 16 is true. Since both age >= 16 and hasConsent are true, the value of canDrive is also true.


More on Booleans

Boolean values with if...else and for loop.

In TypeScript, boolean values are also used for controlling the flow of logic within if...else statements and for loops. For example,

let hasPermission: boolean = true;

// Boolean with if...else
if (hasPermission) {
    console.log("Access granted");
}
else {
    console.log("Access denied");
}

Output

Access granted

Here, if the boolean variable hasPermission is true then Access granted is printed. Otherwise, Access denied is printed.

// Boolean with for loop
for (let i: number = 0; i < 3; i++) {
    console.log("Loop iteration:", i);
}

Output

Loop iteration: 0
Loop iteration: 1
Loop iteration: 2

Here, the boolean expression i < 3 determines the number of iterations inside the for loop.

Some data types are converted to specific boolean values.

In TypeScript, certain data types are automatically converted to boolean values when they are evaluated in a context that requires a boolean, such as in conditional statements.

Here is a list of values that get converted to specific boolean values.

Data Types Boolean Value
undefined false
null false
NaN false
'' false
0 false
20 true
-20 true
"hello" true

TypeScript Boolean() Function

The Boolean() function is used to convert various data types to boolean values. For example,

let a: string = "true";
console.log(Boolean(a)); // true

Everything with a value returns true. For example,

let result: boolean;

// Convert a positive number to boolean
result = Boolean(20);
console.log(result); // true
console.log(typeof result); // boolean

// Convert a negative number to boolean
result = Boolean(-20);
console.log(result); // true

// Convert a string to boolean
result = Boolean("hello");
console.log(result); // true

// Create an object
let obj = {a: 1};

// Convert the object to boolean
result = Boolean(obj)
console.log(result); // true

In TypeScript, undefined, null, 0, NaN, '' converts to false. For example,

let result: boolean;

// Convert empty string to boolean
result = Boolean('');
console.log(result); // false

// Convert 0 to boolean
result = Boolean(0);
console.log(result); // false

// Convert undefined to boolean
result = Boolean(undefined);
console.log(result); // false

// Convert null to boolean
result = Boolean(null);
console.log(result); // false

// Convert NaN to boolean
result = Boolean(NaN);
console.log(result); // false

Note: If you want to learn more about the boolean conversion, visit TypeScript Type Conversion.


Boolean Objects

You can create a boolean object using the new keyword. For example,

let a: boolean = true;

// Create a boolean object
let b = new Boolean(true);

console.log(a); // true
console.log(b); // [Boolean: true]

console.log(typeof a); // "boolean"
console.log(typeof b); // "object"

Output

true
[Boolean: true]
boolean
object

Note: It is recommended to avoid using boolean objects since it slows down the program.


TypeScript Boolean Methods

Some built-in boolean methods in TypeScript are:

Method Description
toString() Returns a string value by converting boolean to a string.
valueOf() Returns the primitive value of a boolean object.

Example: Convert Boolean to String

let count: boolean = false;

// Convert the count variable to string
let result: string = count.toString();

console.log(result);
console.log(typeof result);

Output

false
string

Here, we have converted the boolean variable count to string using the toString() method.

Example: Boolean valueOf() Method

// Create a boolean object
let boolObj = new Boolean(true);

// Get the primitive value of boolObj
let result: boolean = boolObj.valueOf();

console.log(boolObj); //  [Boolean: true]
console.log(result); // true

console.log(typeof boolObj); // "object"
console.log(typeof result); // "boolean"

Output

[Boolean: true]
true
object
boolean

Here, boolObj.valueOf() returns the primitive value of the boolean object boolObj.

Note: A primitive value of a boolean object is just the raw boolean value stored in the object, i.e., true or false.


Also Read:

Did you find this article helpful?

Our premium learning platform, created with over a decade of experience and thousands of feedbacks.

Learn and improve your coding skills like never before.

Try Programiz PRO
  • Interactive Courses
  • Certificates
  • AI Help
  • 2000+ Challenges