Learn TypeScript in Construct, part 3: operators

UpvoteUpvote 3 DownvoteDownvote

Index

Features on these Courses

Stats

184 visits, 233 views

Tools

Translations

This tutorial hasn't been translated.

License

This tutorial is licensed under CC BY-NC 4.0. Please refer to the license text if you wish to reuse, share or remix the content contained within this tutorial.

Published on 23 Jun, 2025. Last updated 24 Jun, 2025

Boolean operators

Remember previously we covered the boolean type, which are values that are either true or false. In this section we'll cover operators that either work on booleans, or produce a boolean result.

Comparisons

TypeScript provides several comparison operators which return a boolean. For example a < b returns true if a is less than b, otherwise it returns false. The comparison operators are:

  • Less than < and greater than >
  • Less than or equals <= and greater than or equals >=
  • Equals == and strict equals ===
  • Not equal != and strict not equal !==

Notice that since = is used for assignment, we have to use something else to test if numbers are actually equal. That's why two or three equals in a row are used, to distinguish comparison from assignment.

Why are there two kinds of equality? In JavaScript, the standard equals == is allowed to convert types, and so can return true even if the types are different. The strict equals === does not convert types, so always returns false if the types are different. You can try this out in the browser console (remember that uses JavaScript only):

// Try entering in to the console:
5 == 5		// true
5 == "5"	// true (converts types)
5 === 5		// true
5 === "5"	// false (types are different)

Similarly != can convert types and !== does not when testing for inequality.

// Try entering in to the console:
5 != "5"	// false (converts types)
5 !== "5"	// true (types are different)

TypeScript inherits this feature from JavaScript, which is why even though TypeScript uses specific types, it still has both == and ===. TypeScript will usually mark an error if you make a comparison between unrelated types. For example both "hello" == 1 and "hello" === 1 are marked as an error in TypeScript code, as it presumes this is a mistake. If you really intend to make such a comparison, then explicitly convert one of the values.

It's generally regarded as best practice to prefer the strict equality comparisons === and !==. This is also a best practice in JavaScript, and in TypeScript generally you want to ensure you are working with specific types and avoid anything that might automatically convert types as that may lead to confusing results. For that reason, the rest of this guide will prefer using strict comparisons.

The less/greater operators don't have strict equivalents, but for those, TypeScript enforces that both sides must be the same type. For example "hello" < 1 is an error in TypeScript code.

It's also worth noting strings can be compared as less or greater. A string is considered less than another string if it would precede it in sort order. For example "apple" < "banana" is true because apple would come before banana in a sorted list (as this comparison is based on an alphabetical sort order).

  • 0 Comments

Want to leave a comment? Login or Register an account!