Last updated: Feb 26, 2024
Reading timeยท3 min
To compare enums, use dot notation to get the value for a specific enum property and compare it to another value.
The values for numeric enums, without provided initial value, are
auto-incrementing integers, starting at 0
.
enum ShirtSizes { Small, Medium, Large, } const small = ShirtSizes.Small; // ๐๏ธ 0 const medium = ShirtSizes.Medium; // ๐๏ธ 1 if (small < medium) { console.log('โ This runs'); } if (small === 0) { console.log('โ This runs'); } const enumKey = ShirtSizes[ShirtSizes.Small]; console.log(enumKey); // ๐๏ธ "Small" if (enumKey === 'Small') { console.log('โ This runs'); }
You can access specific values on an enum by using dot notation.
Use one of the comparison operators to compare the value against another, e.g.
<
(less than) >
(greater than) or ===
(equals).
The if
block is only run if the condition is met.
The example shows a numeric enum, for which we haven't specified an initial value.
enum ShirtSizes { Small, Medium, Large, }
The value of the Small
key is 0
, Medium
= 1
and Large
= 2
.
When using numeric enums, you can use reverse mappings to get the corresponding key based on a value.
enum ShirtSizes { Small, Medium, Large, } const small = ShirtSizes.Small; // ๐๏ธ 0 const medium = ShirtSizes.Medium; // ๐๏ธ 1 const enumKey = ShirtSizes[ShirtSizes.Small]; console.log(enumKey); // ๐๏ธ "Small" const eKey = ShirtSizes[0]; console.log(eKey); // ๐๏ธ Small
The examples use reverse mappings to get the enum key that corresponds to the
value 0
.
Here is an example of how to get an array of the keys and values of a string enum and check if a specific value is contained in the arrays.
enum ShirtSizes { Small = 'SMALL', Medium = 'MEDIUM', Large = 'LARGE', } const keys = Object.keys(ShirtSizes); console.log(keys); // ๐๏ธ ['Small', 'Medium', 'Large'] if (keys.includes('Small')) { console.log('โ This runs'); } const values = Object.values(ShirtSizes); console.log(values); if (values.includes('SMALL' as unknown as ShirtSizes)) { console.log('โ This runs'); }
Enums in TypeScript are real objects, so we are able to use the Object.keys()
and Object.values()
methods to get an array of the enum keys and values.
includes()
method to check if a specific value is contained in the keys and values of the enum but you could adjust this according to your use case.If working with numeric enums, the Object.keys()
method returns an array
containing the enum's keys and values, so you have to filter out the unnecessary
values.
enum ShirtSizes { Small, Medium, Large, } const keys = Object.keys(ShirtSizes).filter((v) => isNaN(Number(v))); console.log(keys); // ๐๏ธ ['Small', 'Medium', 'Large'] if (keys.includes('Small')) { console.log('โ This runs'); } const values = Object.values(ShirtSizes).filter((v) => !isNaN(Number(v))); console.log(values); // ๐๏ธ [0, 1, 2]
The concept with numeric enums is the same, however, we had to add a call to the
filter
method to exclude the unnecessary values from the returned array.
If you need to check if a value exists in an enum, click on the following article.
You can learn more about the related topics by checking out the following tutorials: