How to Compare Enum strings or numbers in typescript?

Typescript Compare Enum strings or numbers  example Typescripts Enum compare: This tutorial explains how we can compare Enum Strings and numbers in Angular and typescript.

In this tutorial, How to compare the String with Enum value in typescript/javascript.

The below examples use the comparison operator !== and === operators for checking enum equality.

You can check my other posts on typescript Enum object.

How to Compare Enum Strings values in Typescript?

In the below example, Enum is declared for color.

Syntax Enum contains a string property name and value.

=== operator is used to check string against with Enum Object.

By default, the triple equal operator is applied to the same two operands of the same type. In the following example, a string and value are checked and compared, and type conversion is used.

export enum Color {
  RED = "#FF0000",
  GREEN = "#008000",
  YELLOW = "#FFFF00",
  BLUE = "#0000FF",
  MAROON = "#800000",
console.log(Color.RED); //#FF0000
function isColorRed(color: string) {
  if (color === Color.RED) {
    return true;
  return false;

console.log(isColorRed("#FF0000")); //true
console.log(isColorRed("RED")); // false

How to Compare Numbers in enum type in Typescript?

By default, enum holds numeric values if a string value is defined.

In the below example, Chars Enum is declared with constants, but not its value. Numbers are assigned (A=0, B=1, C=2, D=3) Here the number is compared against Enum numeric values.

Here is an example

export enum Chars {
console.log(Chars.A); //0
function isA(char: number) {
  if (char === Chars.A) {
    return true;
  return false;

console.log(isA(1)); //false
console.log(isA(0)); // true


To Sum up, Learn How to compare enum strings and values in Typescript with examples.

Join 6,000 subscribers and get a daily digest of full stack tutorials delivered to your inbox directly.No spam ever. Unsubscribe any time.

Similar Posts
You'll get a notification every time a post gets published here.