Overview
This article explains how to use Enums in TypeScript.
What are Enums?
Enums (enumerated types) represent a collection of related values. While many languages implement Enums, JavaScript does not. However, TypeScript supports Enums, enriching the JavaScript experience.
How to Use Enums
Enums can be defined as follows:
|
|
By default, Enums are assigned numerical values, starting from 0. The compiled JavaScript code would look like this:
|
|
You can also assign string values to Enums:
|
|
When comparing string values, you can write as follows:
|
|
Summary
This article explained how to use Enums in TypeScript. By utilizing Enums, you can improve the readability and maintainability of your code.