What is an Enum?

It is a special data type that can be used to define a set of named constants.

Important Note: The "enums" are real objects in TypeScript and they exist at the runtime. So we are able to use the "Object.keys()" and "Object.values()" methods on it.
Important Note: The "const enums" can only use constant enum expression and they are not available at runtime. So when following the examples, make sure you are not declaring as "const enum".

Let's look at an example of an enum object.

enum Alphabets {
  Apple = "A",
  Bat = "B",
  Car = "C",
  Door = "D",
}

String Enums

The example shown above is a string enum. They have strings as keys and values.

How to access all the keys of an enum?

We can use the "Object.keys()" method to get the keys of an enum.

enum Alphabets {
  Apple = "A",
  Bat = "B",
  Car = "C",
  Door = "D",
}

const keys = Object.keys(Alphabets);

console.log(keys); // πŸ‘ˆ ["Apple","Bat","Car","Door"]
string enum keys

How to access all the values of an enum?

For accessing the values of an enum, we will use the "Object.values()" method.

enum Alphabets {
  Apple = "A",
  Bat = "B",
  Car = "C",
  Door = "D",
}

const values = Object.values(Alphabets);

console.log(values); //πŸ‘ˆ ["A","B","C","D"]
string enum values

How to access a specific value in an enum using the key.

For accessing a specific key value, you need to first find the index of the "key" that you are looking for. You can do that by combining the "Object.keys()" method and the "indexOf()" method. See the following code:

enum Alphabets {
  Apple = "A",
  Bat = "B",
  Car = "C",
  Door = "D",
}

const indexOfApple = Object.keys(Alphabets).indexOf("Apple");
console.log(indexOfApple);  //πŸ‘ˆ 0 
string enum specific index

We first retrieved the array of all the keys and then found the index of the key we are looking for. Now in order to get the key's value, we need to retrieve the array of values using the "Object.values()" method and access the specified value using the index we just found.

enum Alphabets {
  Apple = "A",
  Bat = "B",
  Car = "C",
  Door = "D",
}

const indexOfApple = Object.keys(Alphabets).indexOf("Apple");

const valueofApple = Object.values(Alphabets)[indexOfApple];
console.log(valueofApple);  //πŸ‘ˆ "A"
string enum specific value

Numeric Enum

Numeric enums are different from string enums since they store string values with a number key. We can either specify a number key or it will be implicitly given based on the index position in the object. Here is an example of a numeric enum:

enum NumericEnum {
  One,
  Two,
  Three,
  Four,
}
numeric enum

Here is another example of a numeric enum with custom number keys assigned:

enum NumericEnum {
  One = 1,
  Three = 3,
  Five = 5,
}
numeric enum with custom keys

How to access the values in a numeric enum?

We can directly access the value using either the index of the value or the numeric key we have assigned to the value. Let's see examples for both scenarios.

enum NumericEnum {
  One,
  Two,
  Three,
  Four,
}

console.log(NumericEnum[0]); //πŸ‘ˆ One
console.log(NumericEnum[1]); //πŸ‘ˆ Two
console.log(NumericEnum[2]); //πŸ‘ˆ Two
console.log(NumericEnum[3]); //πŸ‘ˆ Three
numeric enum value access

Now, let's see a numeric enum with keys manually assigned to them.

enum NumericEnum {
  One = 1,
  Three = 3,
  Five = 5,
}

console.log(NumericEnum[1]); //πŸ‘ˆ One
console.log(NumericEnum[3]); //πŸ‘ˆ Three
console.log(NumericEnum[5]); //πŸ‘ˆ Five
numeric enum with custom key value access

How to access all the keys or values of a numeric enum?

Simply using the "Object.values()" or "Object.keys()" method will return both the keys and values in a combined array.

enum NumericEnum {
  One,
  Two,
  Three,
  Four,
}

const values = Object.values(NumericEnum);
console.log(values);  //πŸ‘ˆ  ['One', 'Two', 'Three', 'Four', 0, 1, 2, 3]

const keys = Object.keys(NumericEnum);
console.log(keys); //πŸ‘ˆ  ['0', '1', '2', '3', 'One', 'Two', 'Three', 'Four']
numeric enum keys and values

In order to properly, get the values and keys of a numeric enum, we need to filter out the result accordingly.

We can filter the keys by only returning the numeric values since, in numeric enums, all the keys are numbers.

const keys = Object.keys(NumericEnum).filter((v) => isNaN(Number(v)));
console.log(keys); //πŸ‘ˆ [0, 1, 2, 3]
numeric enum key filter

We can filter the values by only returning the non-numeric values.

const values = Object.values(NumericEnum).filter((v) => !isNaN(Number(v)));
console.log(values);  //πŸ‘ˆ ['One', 'Two', 'Three', 'Four']
numeric enum value filter

I hope my article was helpful to understand the methods used to access the values in different TypeScript enums. Check out my blog to see more articles.