TypeScript - Boolean



The TypeScript Boolean types represent logical values like true or false. Logical values are used to control the flow of the execution within the program. As JavaScript offers both Boolean primitive and object types, TypeScript adds a type annotation. We can create a boolean primitive as well boolean object.

We can create a boolean object by using the Boolean() constructor with new keyword.

To convert a non-boolean value to boolean we should use Boolean() function or double NOT (!!) operator. We should not use Boolean constructor with new keyword.

Syntax

To declare a boolean value (primitive) in TypeScript we can use keyword "boolean".

let varName: boolean = true;

In the above syntax, we declare a boolean variable varName and assign the value true.

To create a Boolean object we use the Boolean() constructor with new Keyword.

const varName = new Boolean(value);

In the above syntax, the value is an expression to be converted to Boolean object. The Boolean() constructor with new returns an object containing the boolean value.

For example,

const isTrue = new Boolean(true);

In the above example, isTrue holds value true while isFalse holds the value false.

Type Annotations

The type annotations in TypeScript is optional as TypeScript can infer the types of the variable automatically. We can use the boolean keyword to annotate the types of boolean variables.

let isPresent : boolean = true; // with type annotation
let isPresent = true // without type annotation

Like variables, the function parameters and return type can also be annotated.

Truthy and Falsy Values

In TypeScript, falsy values are the values that are evaluated to false. There are six falsy values –

  • false
  • 0 (zero)
  • Empty string ("")
  • null
  • undefined
  • NaN

The all other values are truthy.

Converting a non-boolean value to boolean

All the above discussed falsy values are converted to false and truthy values are converted to true. To convert a non-boolean value to boolean, we can use the Boolean() function and double NOT (!!) operator.

Using the Boolean() Function

The Boolean() function in TypeScript converts a non-boolean value to boolean. It returns a primitive boolean value.

let varName = Boolean(value);

The value is an expression to be converted to boolean.

Example

In the below example, we use the Boolean() function to convert a non-boolean value to boolean.

const myBoolean1 = Boolean(10);
console.log(myBoolean1); // true
const myBoolean2 = Boolean("");
console.log(myBoolean2); // false

On compiling, the above code will produce the same code in JavaScript. On executing the JavaScript code will produce the following output –

true
false

Using Double NOT (!!) Operator

The double NOT (!!)operator in TypeScript converts a non-boolean value to boolean. It returns a primitive boolean value.

let varName = !!(value);

The value is an expression to be converted to boolean.

Example

In the below example, we use the double NOT (!!) operator to convert a non-boolean value to boolean.

const myBoolean1 = !!(10);
console.log(myBoolean1); // true
const myBoolean2 = !!("");
console.log(myBoolean2); // false

On compiling, the above code will produce the same code in JavaScript. On executing the JavaScript code will produce the following output –

true
false

Boolean Operations

The boolean operations or logical operations in TypeScript can be performed using the three logical operators, logical AND, OR and NOT operators. These operations return a boolean value (true or false).

Example: Logical AND (&&)

In the example below, we defined two boolean variables x and y. Then perform the logical AND (&&) of these variables.

let x: boolean = true;
let y: boolean = false;
let result: boolean = x && y;
console.log(result); // Output: false

On compiling, it will generate the following JavaScript code.

var x = true;
var y = false;
var result = x && y;
console.log(result); // Output: false

The output of the above example code is as follows –

false

Example: Logical OR (||)

In the example below, we perform logical OR (||) operation of the two boolean variables x and y.

let x: boolean = true;
let y: boolean = false;
let result: boolean = x || y;
console.log(result); // Output: true

On compiling, it will generate the following JavaScript code.

var x = true;
var y = false;
var result = x || y;
console.log(result); // Output: true

The output of the above example code is as follows –

true

Example: Logical NOT (!)

The logical NOT (!) operation of the boolean variable isPresent is performed in the below example.

let isPresent: boolean = false;
let isAbsent:boolean = !isPresent;
console.log(isAbsent); // Output: true

On compiling, it will generate the same code in JavaScript.

The output of the above example code is as follows –

true

Conditional Expression with Booleans

Example: If statement

The below example shows how to use the conditional expression with boolean in if else statement.

let age: number = 25;
let isAdult: boolean = age >= 18;
if (isAdult) {
  console.log('You are an adult.');
} else {
  console.log('You are not an adult.');
}

On compiling it will generate the following JavaScript code –

let age = 25;
let isAdult = age >= 18;
if (isAdult) {
    console.log('You are an adult.');
}
else {
    console.log('You are not an adult.');
}

The output of the above example code is as follows –

You are an adult.

Example: Conditional Statement (Ternary Operator)

Try the following example –

let score: number = 80;
let isPassing: boolean = score >= 70;
let result: string = isPassing ? 'Pass' : 'Fail';
console.log(result); // Output: Pass

On compiling, it will generate the following JavaScript code –

let score = 80;
let isPassing = score >= 70;
let result = isPassing ? 'Pass' : 'Fail';
console.log(result); // Output: Pass

The output of the above example code is as follows –

Pass

TypeScript Boolean vs boolean

The Boolean is not same as the boolean type. The Boolean does not refer to the primitive value. Whereas the boolean is primitive data type in TypeScript. You should always use the boolean with lowercase.

Boolean Objects

As we have seen above, we can create a Boolean object consisting boolean value using the Boolean constructor with new keyword. The Boolean wrapper class provides us with different properties and methods to work with boolean values.

Boolean Properties

Here is a list of the properties of Boolean object –

Sr.No. Property & Description
1

constructor

Returns a reference to the Boolean function that created the object.

2

prototype

The prototype property allows you to add properties and methods to an object.

In the following sections, we will have a few examples to illustrate the properties of Boolean object.

Boolean Methods

Here is a list of the methods of Boolean object and their description.

Sr.No. Method & Description
1

toSource()

Returns a string containing the source of the Boolean object; you can use this string to create an equivalent object.

2

toString()

Returns a string of either "true" or "false" depending upon the value of the object.

3

valueOf()

Returns the primitive value of the Boolean object.

In the following sections, we will have a few examples to demonstrate the usage of the Boolean methods.

Advertisements