Accessing Enum Members at Runtime
What you'll learn: How to reference enum values in your running code and what enums actually become after TypeScript compiles them.
Enums aren't just a compile-time feature that disappears—they exist as real JavaScript objects at runtime. This means you can use them in your executing code, not just for type checking.
How to Access Enum Members
You reference enum members using dot notation or bracket notation, just like accessing object properties:
enum Status {
Pending = 0,
Active = 1,
Completed = 2
}
// Dot notation
const myStatus = Status.Active;
console.log(myStatus); // 1
// Bracket notation
const dynamicStatus = Status["Pending"];
console.log(dynamicStatus); // 0
What Enums Compile To
When TypeScript compiles your code, numeric enums become JavaScript objects with bidirectional mappings. String enums become simpler one-way objects:
// This numeric enum:
enum Direction { Up, Down }
// Compiles roughly to:
// { 0: "Up", 1: "Down", Up: 0, Down: 1 }
// This string enum:
enum Color { Red = "RED", Blue = "BLUE" }
// Compiles roughly to:
// { Red: "RED", Blue: "BLUE" }
This means numeric enums let you look up names from values using reverse mapping, while string enums only go one direction.
Why Runtime Access Matters
You can loop over enum values, pass them to functions, store them in databases, or check them in conditional logic—they're real values your program can use and manipulate, not just type annotations.
Key Takeaway: Enums exist as JavaScript objects at runtime, allowing you to access their members with dot or bracket notation and use them as actual values in your executing code.