Bird
Raised Fist0
Typescriptprogramming~10 mins

Enum member access in Typescript - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Enum member access
Define Enum
↓
Access Member by Name
↓
Get Member Value
↓
Use Value in Code
↓
Access Member by Value (Reverse Lookup)
↓
Get Member Name
↓
Use Name in Code
This flow shows how to define an enum, access its members by name to get values, and also access members by value to get names.
Execution Sample
Typescript
enum Color {
  Red = 1,
  Green,
  Blue
}

let c = Color.Green;
console.log(c);
Defines an enum Color with members, accesses the Green member, and prints its value.
Execution Table
StepActionEvaluationResult
1Define enum Color with Red=1, Green=2, Blue=3N/AColor = {1: 'Red', 2: 'Green', 3: 'Blue', Red: 1, Green: 2, Blue: 3}
2Access Color.GreenColor.Green2
3Assign Color.Green to variable cc = 2c = 2
4Print cconsole.log(c)2
5Access Color[2] (reverse lookup)Color[2]'Green'
6Print Color[2]console.log(Color[2])'Green'
💡 All enum members accessed; program ends after printing values.
Variable Tracker
VariableStartAfter Step 3Final
Colorundefined{Red:1, Green:2, Blue:3, 1:'Red', 2:'Green', 3:'Blue'}{Red:1, Green:2, Blue:3, 1:'Red', 2:'Green', 3:'Blue'}
cundefined22
Key Moments - 2 Insights
Why does Color.Green equal 2 instead of 1?
Because Red is explicitly set to 1, Green automatically gets the next number, 2, as shown in execution_table step 1 and 2.
How can we get the name 'Green' from the value 2?
By using reverse lookup Color[2], which returns 'Green' as shown in execution_table step 5.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of Color.Blue?
A2
B3
C1
Dundefined
💡 Hint
Check step 1 where enum members and their values are defined.
At which step is the variable c assigned a value?
AStep 3
BStep 4
CStep 2
DStep 5
💡 Hint
Look at the action column for assignment to c.
If we change Red to 5, what would Color.Green's value be?
A5
B1
C6
D2
💡 Hint
Enum auto-increments from the previous value, see step 1 for how values are assigned.
Concept Snapshot
enum EnumName { Member1 = value1, Member2, ... }
Access member value: EnumName.Member1
Reverse lookup: EnumName[value] returns member name
Members auto-increment if not assigned
Use enum values for readable constants
Full Transcript
This example shows how to define an enum in TypeScript with members Red, Green, and Blue. Red is set to 1 explicitly, so Green and Blue get 2 and 3 automatically. We assign Color.Green to variable c and print it, which outputs 2. We also demonstrate reverse lookup by accessing Color[2] to get the string 'Green'. This helps understand how enum members can be accessed by name or by value.

Practice

(1/5)
1.

Which of the following is the correct way to access a member named Red from an enum called Color?

easy
A. Color.Red
B. Red.Color
C. Color[Red]
D. Red.Color()

Solution

  1. 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.
  2. Step 2: Apply correct syntax to the example

    For enum Color and member Red, the correct access is Color.Red.
  3. Final Answer:

    Color.Red -> Option A
  4. Quick Check:

    Enum member access = Color.Red [OK]
Hint: Use EnumName.Member to access enum values quickly [OK]
Common Mistakes:
  • Reversing the order like Red.Color
  • Using brackets without quotes for string enums
  • Calling enum members as functions
2.

Which of the following is the correct syntax to define a numeric enum named Direction with members Up, Down, Left, and Right?

?
easy
A. enum Direction (Up, Down, Left, Right)
B. enum Direction = { Up, Down, Left, Right }
C. enum Direction { Up, Down, Left, Right }
D. enum Direction: { Up, Down, Left, Right }

Solution

  1. Step 1: Recall enum declaration syntax

    In TypeScript, enums are declared using the keyword enum followed by the name and curly braces containing members.
  2. Step 2: Match correct syntax

    enum Direction { Up, Down, Left, Right } uses enum Direction { Up, Down, Left, Right }, which is the correct syntax.
  3. Final Answer:

    enum Direction { Up, Down, Left, Right } -> Option C
  4. Quick Check:

    Enum declaration uses curly braces {} [OK]
Hint: Use curly braces {} after enum name to list members [OK]
Common Mistakes:
  • Using equals sign (=) after enum name
  • Using colon (:) instead of braces
  • Using parentheses () instead of braces
3.

Given the enum:

enum Status { Active = 1, Inactive, Pending }

What is the output of console.log(Status[2])?

medium
A. "Pending"
B. "Inactive"
C. 2
D. undefined

Solution

  1. Step 1: Understand numeric enum reverse mapping

    Numeric enums in TypeScript create a reverse mapping from value to name. Here, Active = 1, so Inactive is 2, Pending is 3.
  2. Step 2: Access enum member by value

    Status[2] returns the name of the member with value 2, which is Inactive.
  3. Final Answer:

    "Inactive" -> Option B
  4. Quick Check:

    Numeric enum reverse lookup = "Inactive" [OK]
Hint: Numeric enums map values back to names automatically [OK]
Common Mistakes:
  • Assuming string enums have reverse mapping
  • Confusing member names with values
  • Expecting numeric output instead of string
4.

Consider this enum and code snippet:

enum Fruit { Apple = "APPLE", Banana = "BANANA" }
console.log(Fruit["APPLE"]);

What will happen when this code runs?

medium
A. undefined
B. "APPLE"
C. "Apple"
D. SyntaxError

Solution

  1. Step 1: Understand string enum member access

    String enums do not have reverse mapping from value to name like numeric enums.
  2. Step 2: Analyze the code behavior

    Accessing Fruit["APPLE"] looks for a member named "APPLE", but enum keys are "Apple" and "Banana". So it returns undefined.
  3. Final Answer:

    undefined -> Option A
  4. Quick Check:

    String enums lack reverse mapping = undefined [OK]
Hint: String enums do not support reverse lookup by value [OK]
Common Mistakes:
  • Expecting reverse mapping for string enums
  • Confusing enum keys with values
  • Assuming case-insensitive access
5.

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?

hard
A. "Unknown" because reverse mapping is not supported
B. "404" because it returns the number as string
C. Runtime error due to invalid access
D. "NotFound" because numeric enums support reverse mapping

Solution

  1. Step 1: Recognize numeric enum reverse mapping

    Numeric enums in TypeScript create a reverse mapping from numeric values to member names.
  2. Step 2: Evaluate function behavior with input 404

    Calling getStatusName(404) returns Status[404], which is "NotFound". If no match, it returns "Unknown".
  3. Final Answer:

    "NotFound" because numeric enums support reverse mapping -> Option D
  4. Quick Check:

    Numeric enum reverse lookup returns member name [OK]
Hint: Numeric enum values map back to names automatically [OK]
Common Mistakes:
  • Thinking it returns the number as string
  • Assuming reverse mapping doesn't exist
  • Expecting runtime errors on valid enum access
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard