Enums help group related values under one name. Accessing enum members lets you use these values clearly and safely in your code.
Enum member access in Typescript
enum EnumName {
Member1,
Member2 = 5
}
// Access enum member
EnumName.Member1
EnumName[0] // for numeric enumsEnum members can be numbers or strings.
Numeric enums have reverse mapping: you can get name from value.
enum Direction {
Up,
Down,
Left,
Right
}
let move = Direction.Up;enum Status {
Success = 1,
Failure = 0
}
let result = Status.Success;enum Color {
Red = "RED",
Green = "GREEN",
Blue = "BLUE"
}
let favorite = Color.Green;enum Direction {
Up,
Down
}
let name = Direction[0];This program uses enum member access to decide what action to take for each traffic light color.
enum TrafficLight {
Red = "RED",
Yellow = "YELLOW",
Green = "GREEN"
}
function getAction(light: TrafficLight): string {
switch (light) {
case TrafficLight.Red:
return "Stop";
case TrafficLight.Yellow:
return "Get Ready";
case TrafficLight.Green:
return "Go";
}
}
console.log(getAction(TrafficLight.Red));
console.log(getAction(TrafficLight.Yellow));
console.log(getAction(TrafficLight.Green));Use enum member names to avoid mistakes with raw values.
String enums do not have reverse mapping like numeric enums.
Accessing enum members by name is safer and clearer than using raw values.
Enums group related values under one name.
Access enum members using dot notation: EnumName.Member.
Numeric enums support reverse mapping; string enums do not.
Practice
Which of the following is the correct way to access a member named Red from an enum called Color?
Solution
Step 1: Understand enum member access syntax
In TypeScript, enum members are accessed using dot notation with the enum name first, then the member name.Step 2: Apply correct syntax to the example
For enumColorand memberRed, the correct access isColor.Red.Final Answer:
Color.Red -> Option AQuick Check:
Enum member access = Color.Red [OK]
- Reversing the order like Red.Color
- Using brackets without quotes for string enums
- Calling enum members as functions
Which of the following is the correct syntax to define a numeric enum named Direction with members Up, Down, Left, and Right?
?
Solution
Step 1: Recall enum declaration syntax
In TypeScript, enums are declared using the keywordenumfollowed by the name and curly braces containing members.Step 2: Match correct syntax
enum Direction { Up, Down, Left, Right } usesenum Direction { Up, Down, Left, Right }, which is the correct syntax.Final Answer:
enum Direction { Up, Down, Left, Right } -> Option CQuick Check:
Enum declaration uses curly braces {} [OK]
- Using equals sign (=) after enum name
- Using colon (:) instead of braces
- Using parentheses () instead of braces
Given the enum:
enum Status { Active = 1, Inactive, Pending }What is the output of console.log(Status[2])?
Solution
Step 1: Understand numeric enum reverse mapping
Numeric enums in TypeScript create a reverse mapping from value to name. Here,Active = 1, soInactiveis 2,Pendingis 3.Step 2: Access enum member by value
Status[2]returns the name of the member with value 2, which isInactive.Final Answer:
"Inactive" -> Option BQuick Check:
Numeric enum reverse lookup = "Inactive" [OK]
- Assuming string enums have reverse mapping
- Confusing member names with values
- Expecting numeric output instead of string
Consider this enum and code snippet:
enum Fruit { Apple = "APPLE", Banana = "BANANA" }
console.log(Fruit["APPLE"]);What will happen when this code runs?
Solution
Step 1: Understand string enum member access
String enums do not have reverse mapping from value to name like numeric enums.Step 2: Analyze the code behavior
AccessingFruit["APPLE"]looks for a member named "APPLE", but enum keys are "Apple" and "Banana". So it returnsundefined.Final Answer:
undefined -> Option AQuick Check:
String enums lack reverse mapping = undefined [OK]
- Expecting reverse mapping for string enums
- Confusing enum keys with values
- Assuming case-insensitive access
You have this enum and function:
enum Status { Success = 200, NotFound = 404, ServerError = 500 }
function getStatusName(code: number): string {
return Status[code] || "Unknown";
}What will getStatusName(404) return, and why?
Solution
Step 1: Recognize numeric enum reverse mapping
Numeric enums in TypeScript create a reverse mapping from numeric values to member names.Step 2: Evaluate function behavior with input 404
CallinggetStatusName(404)returnsStatus[404], which is "NotFound". If no match, it returns "Unknown".Final Answer:
"NotFound" because numeric enums support reverse mapping -> Option DQuick Check:
Numeric enum reverse lookup returns member name [OK]
- Thinking it returns the number as string
- Assuming reverse mapping doesn't exist
- Expecting runtime errors on valid enum access
Ready to prove it?
Pass the test and this pattern moves unknown
