How to Convert Number to Boolean in TypeScript (With Practical Examples)

Recently, one of my team members was trying to convert a number to a boolean in TypeScript. I suggest a few methods to do so.

In this tutorial, I will explain several methods to convert a number to a boolean in TypeScript with practical examples.

In TypeScript, you’ll often encounter situations where you need to interpret a number as a boolean value. For example:

  • API Responses: Some APIs return 0 or 1 instead of false or true.
  • Feature Toggles: You might use numbers to represent feature flags in your application.
  • User Input: Form fields might return numeric values that you want to treat as booleans.

Now, let me show you all the methods for converting a number to a boolean in TypeScript.

Method 1: Using Double Negation (!!)

The most concise and popular way to convert a number to a boolean in TypeScript is with the double negation operator (!!). Here’s how it works:

const value: number = 1;
const isActive: boolean = !!value; // true
console.log(isActive);

const anotherValue: number = 0;
const isAvailable: boolean = !!anotherValue; // false
console.log(isAvailable);

You can see the exact output in the screenshot below:

Convert Number to Boolean in TypeScript

Why I like this method:
It’s fast, easy to read, and works perfectly for most scenarios. If you’re coming from a JavaScript background, this will feel very familiar.

Check out Convert Number to String with Leading Zeros in TypeScript

Method 2: Using the Boolean Constructor

Another straightforward approach is to use the Boolean() constructor for converting a number to a string in TypeScript.

Here is an example.

const value: number = 5;
const isEnabled: boolean = Boolean(value); // true
console.log(isEnabled);

const zeroValue: number = 0;
const isDisabled: boolean = Boolean(zeroValue); // false
console.log(isDisabled);

I executed the above TypeScript code using VS Code and you can see the exact output in the screenshot below:

TypeScript Convert Number to Boolean

This method is just as effective as double negation and can be more readable for beginners, especially when explaining code to team members.

Check out Round Down to 2 Decimals in TypeScript

Method 3: Using Ternary Operator

If you want more control, especially when handling edge cases (like only treating 1 as true and everything else as false), the ternary operator is your friend:

Here is an example.

const flag: number = 1;
const isFlagSet: boolean = flag === 1 ? true : false; // true
console.log(isFlagSet);

const flag2: number = 2;
const isFlagSet2: boolean = flag2 === 1 ? true : false; // false
console.log(isFlagSet2);

You can see the exact output in the screenshot below:

How to Convert Number to Boolean in TypeScript

I use this approach when I need to enforce stricter logic, such as only accepting 1 as true (common in database or API integrations in US-based systems).

Check out How to Round to 2 Decimals in TypeScript?

Method 4: Custom Utility Function

For more complex scenarios, or when you want to reuse the logic throughout your codebase, creating a utility function is a great option. Below is an example:

function numberToBoolean(num: number): boolean {
  return num !== 0;
}

// Usage
const loginAttempts: number = 0;
const isLockedOut: boolean = numberToBoolean(loginAttempts); // false

const seatsAvailable: number = 10;
const canBook: boolean = numberToBoolean(seatsAvailable); // true

Pro tip:
Custom functions make your code more readable and maintainable, especially in larger TypeScript projects.

Read Convert Typescript Dictionary to String

Examples:

Now, let me show you some examples from the projects I have worked on in TypeScript.

1. Payment Status from an API

Many US-based payment gateways return numeric status codes. For example:

const paymentStatus: number = 1; // 1 = Paid, 0 = Unpaid
const isPaid: boolean = !!paymentStatus; // true

2. Feature Flags for US Customers

Suppose you have a feature flag system where 1 means enabled for US customers:

const usFeatureFlag: number = 1;
const isFeatureEnabled: boolean = Boolean(usFeatureFlag); // true

3. User Consent

Storing user consent as 0 (not given) or 1 (given):

const userConsent: number = 1;
const hasConsented: boolean = userConsent === 1 ? true : false; // true

Conclusion

In this tutorial, I explained how to convert numbers to booleans in TypeScript using various methods with examples. By using methods like double negation, the Boolean constructor, ternary operators, or custom utility functions, you can achieve this.

I hope the examples explained above are helpful to you.

You may also like the following tutorials:

Power Apps functions free pdf

30 Power Apps Functions

This free guide walks you through the 30 most-used Power Apps functions with real business examples, exact syntax, and results you can see.

Download User registration canvas app

DOWNLOAD USER REGISTRATION POWER APPS CANVAS APP

Download a fully functional Power Apps Canvas App (with Power Automate): User Registration App