Bird
Raised Fist0
Typescriptprogramming~10 mins

Enum member access in Typescript - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to access the enum member.

Typescript
enum Color { Red, Green, Blue }
const favorite = Color.[1];
Drag options to blanks, or click blank then click option'
AYellow
BRed
CPink
DOrange
Attempts:
3 left
💡 Hint
Common Mistakes
Using a name not declared in the enum.
Trying to access enum members as strings.
2fill in blank
medium

Complete the code to get the numeric value of the enum member.

Typescript
enum Direction { Up = 1, Down, Left, Right }
const dirValue = Direction.[1];
Drag options to blanks, or click blank then click option'
AUp
BSideways
CBackward
DForward
Attempts:
3 left
💡 Hint
Common Mistakes
Using a member name not in the enum.
Confusing string values with numeric enum values.
3fill in blank
hard

Fix the error in accessing the enum member.

Typescript
enum Status { Active = 'ACTIVE', Inactive = 'INACTIVE' }
const currentStatus = Status.[1];
Drag options to blanks, or click blank then click option'
AInactivee
Bactive
CACTIVE
DInactive
Attempts:
3 left
💡 Hint
Common Mistakes
Using lowercase or uppercase strings instead of enum member names.
Misspelling the enum member name.
4fill in blank
hard

Fill both blanks to access the enum member and get its value.

Typescript
enum Fruit { Apple = 5, Banana = 10 }
const fruitName = Fruit.[1];
const fruitValue = Fruit.[2];
Drag options to blanks, or click blank then click option'
AApple
BBanana
COrange
DGrape
Attempts:
3 left
💡 Hint
Common Mistakes
Using names not declared in the enum.
Mixing up member names and values.
5fill in blank
hard

Fill all three blanks to create an enum, access a member, and get its value.

Typescript
enum Vehicle { [1] = 1, [2] = 2, [3] = 3 }
const myVehicle = Vehicle.Car;
Drag options to blanks, or click blank then click option'
ACar
BBike
CTruck
DPlane
Attempts:
3 left
💡 Hint
Common Mistakes
Using invalid or duplicate member names.
Not assigning values to enum members.

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